Android开发:UnsupportedOperationException异常:addView(查看,的LayoutParams)不支持适配器视图 [英] Android dev: UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
问题描述
我想创建Android中简单的应用程序,我仍然得到这些错误:
i trying to create simple app in android and i'm still getting these error:
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
我尝试谷歌它并修复通过一些要求在这里,但unsuccessfuly:(
i try to google it and fix via some asks here, but unsuccessfuly :(.
我主要活动有ListView控件, 在我的onCreate调用
I have Main Activity with ListView, in onCreate i call
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);
}
});
这code应该开始新的活动,ItemDetail,其中code是在这里:
this code should start new activity, ItemDetail, which code is here:
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);
我仍然得到这个错误:
i'm still get this error:
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)
对于第二个(具体活动)布局文件:
layout file for second (detail activity):
<?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>
许多感谢的任何建议
推荐答案
您的XML文件包含一个ListView的儿童观,你不能做到这一点。您的XML文件应该是链接才可这样的:
Your XML file contains Views that are children of a ListView, you cannot do this. Your XML file should look liks this:
<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>
这篇关于Android开发:UnsupportedOperationException异常:addView(查看,的LayoutParams)不支持适配器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!