Logcat消息:
> 11-27 05:33:47.706: E/AndroidRuntime(2417): FATAL EXCEPTION: main
11-27 05:33:47.706: E/AndroidRuntime(2417): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
11-27 05:33:47.706: E/AndroidRuntime(2417): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-27 05:33:47.706: E/AndroidRuntime(2417): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-27 05:33:47.706: E/AndroidRuntime(2417): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-27 05:33:47.706: E/AndroidRuntime(2417): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-27 05:33:47.706: E/AndroidRuntime(2417): at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 05:33:47.706: E/AndroidRuntime(2417): at android.os.Looper.loop(Looper.java:137)
11-27 05:33:47.706: E/AndroidRuntime(2417): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-27 05:33:47.706: E/AndroidRuntime(2417): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 05:33:47.706: E/AndroidRuntime(2417): at java.lang.reflect.Method.invoke(Method.java:525)
11-27 05:33:47.706: E/AndroidRuntime(2417): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-27 05:33:47.706: E/AndroidRuntime(2417): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-27 05:33:47.706: E/AndroidRuntime(2417): at dalvik.system.NativeStart.main(Native Method)
11-27 05:33:47.706: E/AndroidRuntime(2417): Caused by: java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
11-27 05:33:47.706: E/AndroidRuntime(2417): at android.widget.AdapterView.addView(AdapterView.java:451)
11-27 05:33:47.706: E/AndroidRuntime(2417): at com.example.calculator.MainActivity.onCreate(MainActivity.java:46)
11-27 05:33:47.706: E/AndroidRuntime(2417): at android.app.Activity.performCreate(Activity.java:5133)
11-27 05:33:47.706: E/AndroidRuntime(2417): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-27 05:33:47.706: E/AndroidRuntime(2417): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-27 05:33:47.706: E/AndroidRuntime(2417): ... 11 more
代码:
`super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GridView)findViewById(R.id.keys);
Button b = new Button(this);
b.setText("1");
gv.addView(b);`
我在gv.addView(b)
XML的文件:enter code here
android:useDefaultMargins="true"
android:alignmentMode="alignBounds"
android:columnOrderPreserved="false"
android:columnCount="1"
android:rowCount="3"
tools:context=".MainActivity" >
<EditText
android:id="@+id/resBox"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_gravity="left"
android:background="@color/grey"
android:textColor="@color/black"
android:hint="@string/nothing"
android:gravity="right" />
<EditText
android:id="@+id/calBox"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_column="0"
android:layout_gravity="left"
android:background="@color/darkGrey"
android:ems="10"
android:gravity="right"
android:textColor="@color/white"
android:hint="@string/zero" />
<GridView
android:id="@+id/keys"
android:layout_width="match_parent"
android:layout_height="232dp"
android:layout_column="0"
android:layout_gravity="center_horizontal|bottom"
android:layout_row="2"
android:numColumns="4"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:clipChildren="true" />
`
答案 0 :(得分:2)
要向GridView添加视图,您需要创建适配器并使用它。有关更多详细信息,请参阅以下示例:
答案 1 :(得分:1)
GridVIew与列表视图非常相似,我们不能通过调用addView()向其添加任何项目,在初始API之后它不受支持。相反,我们应该定义一个适配器并将gridview设置为它
&#34; void addView(查看子项) 不支持此方法,并在调用时抛出UnsupportedOperationException。&#34;
http://developer.android.com/reference/android/widget/ListView.html