我正在尝试扩展基本适配器并返回按钮视图。该应用程序在开始时崩溃。下面显示的第一部分代码是创建类型按钮的数组。第二组代码使用自定义基本适配器。
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;
}
ERROR
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)