gpt4 book ai didi

android - AdapterView 不支持 UnsupportedOperationException : addView(View, LayoutParams)

转载 作者:行者123 更新时间:2023-11-29 14:20:46 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的 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

我尝试用谷歌搜索并通过此处的一些答案进行修复,但没有解决 :(。

我有一个带有 ListViewMainActivity,在 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);

}
});

这段代码应该开始新的 Activity ,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)

第二个布局文件(详细 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 的 subview 的 View ,您不能这样做。您的 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>

关于android - AdapterView 不支持 UnsupportedOperationException : addView(View, LayoutParams),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18728453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com