Android 由于java.lang.UnsupportedOperationException,应用程序在启动时崩溃:AdapterView中不支持addView(视图,LayoutParams)

Android 由于java.lang.UnsupportedOperationException,应用程序在启动时崩溃:AdapterView中不支持addView(视图,LayoutParams),android,android-adapterview,Android,Android Adapterview,我的应用程序每次在模拟器中启动时都会崩溃。以下是日志: 01-02 17:20:58.859: ERROR/AndroidRuntime(249): Uncaught handler: thread main exiting due to uncaught exception 01-02 17:20:58.889: ERROR/AndroidRuntime(249): java.lang.RuntimeException: Unable to start activity ComponentI

我的应用程序每次在模拟器中启动时都会崩溃。以下是日志:

01-02 17:20:58.859: ERROR/AndroidRuntime(249): Uncaught handler: thread main exiting due to uncaught exception

01-02 17:20:58.889: ERROR/AndroidRuntime(249): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shoppinglist/com.shoppinglist.ShoppingList}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

01-02 17:20:58.889: ERROR/AndroidRuntime(249):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)

01-02 17:20:58.889: ERROR/AndroidRuntime(249):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)

01-02 17:20:58.889: ERROR/AndroidRuntime(249):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)

01-02 17:20:58.889: ERROR/AndroidRuntime(249):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)

01-02 17:20:58.889: ERROR/AndroidRuntime(249):     at android.os.Handler.dispatchMessage(Handler.java:99)

01-02 17:20:58.889: ERROR/AndroidRuntime(249):     at android.os.Looper.loop(Looper.java:123)

01-02 17:20:58.889: ERROR/AndroidRuntime(249):     at android.app.ActivityThread.main(ActivityThread.java:4203)

01-02 17:20:58.889: ERROR/AndroidRuntime(249):     at java.lang.reflect.Method.invokeNative(Native Method)

01-02 17:20:58.889: ERROR/AndroidRuntime(249):     at java.lang.reflect.Method.invoke(Method.java:521)

01-02 17:20:58.889: ERROR/AndroidRuntime(249):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)

01-02 17:20:58.889: ERROR/AndroidRuntime(249):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)

01-02 17:20:58.889: ERROR/AndroidRuntime(249):     at dalvik.system.NativeStart.main(Native Method)**
这是我的.java文件:

package com.shoppinglist;

import android.app.ListActivity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ShoppingList extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button1main = (Button) findViewById(R.id.Button01main);
     button1main.setOnClickListener(new OnClickListener()  {
     @Override
      public void onClick(View v)  {
       final Dialog dialog = new Dialog(ShoppingList.this);
       dialog.setContentView(R.layout.maindialog);
       dialog.setCancelable(true);
       Button button = (Button) dialog.findViewById(R.id.cancel);
       button.setOnClickListener(new OnClickListener()  {
        @Override
         public void onClick(View v)  {
          dialog.dismiss();
        }
        });
       dialog.show();
       }
     });
    }
}
My layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFCC00"
    android:padding="10dp"
    android:id="@+id/listview">
        <Button
            android:id="@+id/Button01main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
   android:text="Add item..."/>
</ListView> 

我的对话框xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/layout_root"
  android:orientation="vertical"
  android:background="#FFFFFF"
  android:minHeight="100dp"
  android:minWidth="300dp">
  <EditText
    android:id="@+id/edittext"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:hint="Type the name of an item here...">
   </EditText>
 <LinearLayout
  android:id="@+id/button_layout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal"
  android:layout_gravity="right">
  <Button
   android:id="@+id/ok"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="OK"
   android:layout_gravity="bottom">
   </Button>
  <Button
   android:id="@+id/cancel"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Cancel"
   android:layout_gravity="bottom">
   </Button>
  </LinearLayout> 
</LinearLayout>


任何帮助都将不胜感激。我需要修复的代码示例,我不是很有经验。

在布局xml中,您试图在ListView中添加一个按钮。ListView是一个AdapterView,正如错误所说,因此它必须由一个适配器填充,该适配器提供一个类似“事物”的列表-这些可以是字符串列表(来自数组列表)或您创建的自定义视图列表,但它们基本上都是相同类型的事物-例如,您不能添加按钮,然后给另一个添加一个字符串。创建一个适配器以在ListView中提供所需的内容。

在布局xml中,您试图在ListView中添加一个按钮。ListView是一个AdapterView,正如错误所说,因此它必须由一个适配器填充,该适配器提供一个类似“事物”的列表-这些可以是字符串列表(来自数组列表)或您创建的自定义视图列表,但它们基本上都是相同类型的事物-例如,您不能添加按钮,然后给另一个添加一个字符串。创建一个适配器,在ListView中提供您想要的东西。

作为一个相对的n00b,我必须说Android的ListView实现似乎需要一些可笑的晦涩技巧才能使其工作。我想这对你们大多数人来说可能是显而易见的,但既然我两天来一直在试图弄清楚为什么会出现同样的错误,我认为这太复杂了。坚持下去Bennett——我们都是从n00b开始的。我必须说——作为一个相对的n00b——Android的ListView实现似乎需要一些可笑的晦涩技巧才能让它工作。我想这对你们大多数人来说可能是显而易见的,但由于我两天来一直在试图弄清楚为什么我会出现同样的错误,我认为这太复杂了。坚持下去Bennett-我们都是从N00B开始的。