Android 自定义listview错误:java.lang.unsupportedoperationexception:addview

Android 自定义listview错误:java.lang.unsupportedoperationexception:addview,android,android-listview,android-view,android-adapter,android-adapterview,Android,Android Listview,Android View,Android Adapter,Android Adapterview,我对android真的是个新手,我知道你不应该粘贴整个代码,但我真的不知道问题出在哪里 我试图创建一个有点自定义的列表视图,包含一个文本和一个图像,但是一直遇到这个异常,不知道如何解决它 02-13 13:23:06.477:E/AndroidRuntime(1834):java.lang.UnsupportedOperationException:addView(视图,布局参数)在AdapterView中不受支持 所以我希望这里有人能帮我。这就是我的启动器活动的样子 public class

我对android真的是个新手,我知道你不应该粘贴整个代码,但我真的不知道问题出在哪里

我试图创建一个有点自定义的列表视图,包含一个文本和一个图像,但是一直遇到这个异常,不知道如何解决它

02-13 13:23:06.477:E/AndroidRuntime(1834):java.lang.UnsupportedOperationException:addView(视图,布局参数)在AdapterView中不受支持

所以我希望这里有人能帮我。这就是我的启动器活动的样子

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ListView list=(ListView) this.findViewById(R.id.list_view);



        int[] stringIds=new int[2];
        stringIds[0]=R.string.text1;
        stringIds[1]=R.string.text2;

        int[] imgIds=new int[2];
        imgIds[0]=R.drawable.ic_launcher;
        imgIds[1]=R.drawable.ic_launcher;

        CustomAdapter custom=new CustomAdapter(this, stringIds, imgIds);
        list.setAdapter(custom);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
我的自定义适配器:

public class CustomAdapter extends BaseAdapter {

    Context context;
    int[] stringIds;
    int[] imgIds;
    public CustomAdapter(Context context, int[]stringIds, int[] imgIds) {

        this.stringIds=stringIds;
        this.imgIds=imgIds;
        this.context=context;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View finalView=convertView;
        if(finalView==null){
             LayoutInflater inflater=((Activity)context).getLayoutInflater();
             finalView= inflater.inflate(R.layout.list_view_row, parent);
        } 

        TextView txt=(TextView)finalView.findViewById(R.id.list_view_row_text_view);
        ImageView img=(ImageView)finalView.findViewById(R.id.imageView1);


        img.setImageResource(imgIds[position]);

        return finalView;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return this.stringIds.length;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;

    }
}
activity_main.xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>
<?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" >

   <TextView 
       android:id="@+id/list_view_row_text_view"
        android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" />


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

列表\视图\行.xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>
<?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" >

   <TextView 
       android:id="@+id/list_view_row_text_view"
        android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" />


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

此处:

finalView= inflater.inflate(R.layout.list_view_row, parent);
删除父项

finalView= inflater.inflate(R.layout.list_view_row, null);

也使用

setContentView(R.layout.activity_main);
在得到

列表视图