Java 在Android中创建自定义ListView时不支持操作异常

Java 在Android中创建自定义ListView时不支持操作异常,java,android,android-layout,Java,Android,Android Layout,我想有一个自定义的listView,所以我这样做了: 创建了一个活动: public class PRS_MainList_Act extends ListActivity { MainListAdapter mladp; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCre

我想有一个自定义的listView,所以我这样做了:

创建了一个活动:

public class PRS_MainList_Act extends ListActivity {
    MainListAdapter mladp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prs__main_list_);
        ArrayList<MainMenuItem> menuItems = new ArrayList<MainMenuItem>();
        menuItems.add(new MainMenuItem(getResources().getString(R.string.main_list_connectivity), getResources().getString(R.string.main_list_connectivityDesc)));
        this.mladp = new MainListAdapter(this, R.layout.man_list_item, R.id.textView1, menuItems);
        setListAdapter(mladp);

    }
}
自定义阵列适配器:

public class MainListAdapter extends ArrayAdapter<MainMenuItem> {

    ArrayList<MainMenuItem> menuItems;

    public MainListAdapter(Context context, int resource,
            int textViewResourceId, ArrayList<MainMenuItem> menuItems) {
        super(context, resource, textViewResourceId, menuItems);
        this.menuItems = menuItems;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.man_list_item, parent);
        }
        MainMenuItem it = menuItems.get(position);
        if (it != null) {
            TextView titleTV = (TextView) view.findViewById(R.id.textView1);
            TextView descriptionTV = (TextView) view.findViewById(R.id.textView2);
            ImageView iconIV = (ImageView)view.findViewById(R.id.imageView1);
            if (titleTV != null) {
                titleTV.setText(it.getItemText());
            }
            if(descriptionTV != null){
                descriptionTV.setText(it.getItemDescription());
            }
            if(iconIV != null){
                if(it.getItemText().equals(getContext().getResources().getString(R.string.main_list_connectivity)))
                    iconIV.setImageResource(R.drawable.network_connections);
            }
        }
        return view;
    }

}
public类MainListAdapter扩展了ArrayAdapter{
ArrayList菜单项;
公共MainListAdapter(上下文、int资源、,
int textViewResourceId,ArrayList菜单项){
超级(上下文、资源、textViewResourceId、菜单项);
this.menuItems=menuItems;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=转换视图;
if(convertView==null){
LayoutInflater vi=(LayoutInflater)getContext().getSystemService(
上下文。布局(充气机和服务);
视图=vi.充气(R.布局.人员列表项目,父项);
}
main menuitem it=menuItems.get(位置);
如果(it!=null){
TextView titleTV=(TextView)view.findViewById(R.id.textView1);
TextView descriptionTV=(TextView)view.findViewById(R.id.textView2);
ImageView iconIV=(ImageView)view.findViewById(R.id.imageView1);
if(titleTV!=null){
titleTV.setText(it.getItemText());
}
if(descriptionTV!=null){
descriptionTV.setText(it.getItemDescription());
}
如果(iconIV!=null){
if(it.getItemText().equals(getContext().getResources().getString(R.string.main\u list\u connectivity)))
iconIV.setImageResource(R.drawable.network_connections);
}
}
返回视图;
}
}
以下是我的活动布局和项目布局:

<LinearLayout 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=".PRS_MainList_Act" >
    <ListView android:id="@android:id/list"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

</LinearLayout>

项目布局:

<?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="horizontal" >

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="TextView" />

    </LinearLayout>

</LinearLayout>

我得到的错误是:java.lang.UnsupportedOperationException:addView(视图,LayoutParams)在AdapterView中不受支持


提前感谢您的帮助。

您不能使用此版本的
充气方法:

view = vi.inflate(R.layout.man_list_item, parent);
因为这会将膨胀的
视图添加到父视图中,这在
列表视图中是不允许的。请改用以下版本:

view = vi.inflate(R.layout.man_list_item, parent, false);

这将使视图膨胀,但不会将其添加到父视图中。此版本非常重要,因为它将为膨胀视图提供适当的
布局参数。

能否包含完整堆栈跟踪?Adam,Luksprog解决方案已成功。是否需要包含完整的堆栈跟踪?
view = vi.inflate(R.layout.man_list_item, parent, false);