3

我对android真的很陌生,我知道你不应该粘贴整个代码,但老实说我不知道​​问题出在哪里。

我试图创建一个带有文本和图像的自定义列表视图,但不断收到此异常并且不知道如何解决它

02-13 13:23:06.477: E/AndroidRuntime(1834): java.lang.UnsupportedOperationException: addView(View, LayoutParams) 在 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>

list_view_row.xml 布局

<?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>
4

1 回答 1

3

这里:

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

删除父级

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

或者

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

也使用

setContentView(R.layout.activity_main);

在得到之前

列表显示

于 2013-02-13T13:50:03.617 回答