Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在动态按钮中使用自定义适配器_Java_Android - Fatal编程技术网

Java 在动态按钮中使用自定义适配器

Java 在动态按钮中使用自定义适配器,java,android,Java,Android,我试图扩展基本适配器并返回一个按钮视图。应用程序一开始就崩溃了。下面显示的代码的第一部分是创建button类型的数组。第二组代码使用自定义基本适配器 GridView gv = (GridView) findViewById(R.id.gridview); for (int index = 0; index < Item.length; index++) { // TableRow tr=new TableRow(this); myButt

我试图扩展基本适配器并返回一个按钮视图。应用程序一开始就崩溃了。下面显示的代码的第一部分是创建button类型的数组。第二组代码使用自定义基本适配器

GridView gv = (GridView) findViewById(R.id.gridview);



    for (int index = 0; index < Item.length; index++) {
        //  TableRow tr=new TableRow(this);

        myButton[index] = new Button(this); //initialize the button here
        myButton[index].setText(Item[index]);
        myButton[index].setWidth(120);
        myButton[index].setHeight(120);
        myButton[index].setId(index);
        //myButton[index].setTag(index);
      //  scrViewButLay.addView(myButton[index]);
       // gv.addView(myButton[index]);
        myButton[index].setOnClickListener(getOnClickDoSomething(myButton[index]));

    }
    gv.setAdapter(  new CustomGridAdapter( this, myButton ) );


public class CustomGridAdapter extends BaseAdapter {

private Context context;
private final Button[] gridValues;

//Constructor to initialize values
public CustomGridAdapter(Context context, Button[ ] gridValues) {

    this.context        = context;
    this.gridValues     = gridValues;
}

@Override
public int getCount() {

    // Number of times getView method call depends upon gridValues.length
    return gridValues.length;
}

@Override
public Object getItem(int position) {

    return null;
}

@Override
public long getItemId(int position) {

    return 0;
}


// Number of times getView method call depends upon gridValues.length

public View getView(int position, View convertView, ViewGroup parent) {

    // LayoutInflator to call external grid_item.xml file

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {

        gridView = new View(context);

        // get layout from grid_item.xml ( Defined Below )

        gridView = inflater.inflate( R.layout.starters , null);



        Button b0 = (Button) gridView.findViewById(R.id.button2);

       // String arrLabel = gridValues[ position ];



    } else {

        gridView = (View) convertView;
    }

    return gridView;
}

请发布日志。我在上面添加了问题中的错误以显示请参见问题
06-30 04:44:23.672 7633-7633/com.CITAQ.peripheraltest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.CITAQ.peripheraltest, PID: 7633
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.CITAQ.peripheraltest/com.CITAQ.peripheraltest.MainActivity}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:135)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5219)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
                                                                         Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
                                                                            at android.widget.AdapterView.addView(AdapterView.java:482)
                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
                                                                            at android.app.Activity.setContentView(Activity.java:2153)
                                                                            at com.CITAQ.peripheraltest.MainActivity.onCreate(MainActivity.java:63)
                                                                            at android.app.Activity.performCreate(Activity.java:5976)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358) 
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:135) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5219) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)