我正在尝试创建一个简单的Android应用,我收到此错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twista.shopinglist/com.twista.shopinglist.ItemDetail}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
我尝试谷歌并通过这里的一些答案修复,但不成功它没有解决:(。
我有MainActivity
ListView
,onCreate()
我打电话
public class MainActivity extends Activity {
....
@Override
protected void onCreate(Bundle savedInstanceState) {
...
final ListView listView = (ListView) findViewById(R.id.overListView);
List<Item> values = dataSource.getItems();
final ArrayAdapter<Item> adapter = new ArrayAdapter<Item>(this,
android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item i = adapter.getItem(position);
Intent intent = new Intent(MainActivity.this, ItemDetail.class);
Bundle bundle = new Bundle();
bundle.putLong("my_id", i.getId());
intent.putExtras(bundle);
startActivity(intent);
}
});
此代码应启动新活动ItemDetail
,其代码位于此处:
public class ItemDetail extends Activity {
private ItemsDataSource dataSource;
private long itemId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
Bundle bundle = getIntent().getExtras();
itemId = bundle.getLong("my_id");
Intent intent = getIntent();
dataSource = new ItemsDataSource(this);
try {
dataSource.open();
} catch (SQLException e) {
e.printStackTrace();
}
ListView listView = (ListView) findViewById(R.id.detailListView);
List<SubItem> values = dataSource.getSubItemOfItem(itemId);
ArrayAdapter<SubItem> adapter = new ArrayAdapter<SubItem>(this,
android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
我仍然会收到此错误:
09-10 22:30:30.555 10695-10695/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twista.shopinglist/com.twista.shopinglist.ItemDetail}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
at android.widget.AdapterView.addView(AdapterView.java:477)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:750)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:323)
at android.app.Activity.setContentView(Activity.java:1881)
at com.twista.shopinglist.ItemDetail.onCreate(ItemDetail.java:27)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
第二个布局文件(详细活动):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/detailListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<LinearLayout
android:id="@+id/groupSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/add_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add New"
android:onClick="onClick"/>
<Button
android:id="@+id/delete_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete First"
android:onClick="onClick"/>
</LinearLayout>
</ListView>
非常感谢您的任何建议。
答案 0 :(得分:13)
您的XML文件包含作为ListView子项的视图,您无法执行此操作。你的XML文件应该看起来像这样:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:id="@+id/detailListView" />
<LinearLayout
android:id="@+id/groupSub"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/add_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add New"
android:onClick="onClick"/>
<Button
android:id="@+id/delete_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete First"
android:onClick="onClick"/>
</LinearLayout>
</LinearLayout>