自定义列表提供UnsupportedOperationException

时间:2013-09-16 21:26:49

标签: android list custom-lists

我正在编写一个使用自定义列表的活动。在列表的每一行中,我想要一个图像视图,一个文本视图,最后一个图像按钮。下面是布局的xml

<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="wrap_content"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:layout_marginTop="9dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="5dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

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

</LinearLayout>

现在java文件中的代码:

public class Contacts extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts);

    setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,            R.id.textView1, getResources().getStringArray(R.array.main_menu)));
}

private class MyAdapter extends ArrayAdapter<String> // Adapter Class to create a custom list
{
    // Constructor
    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] strings)
    {
        // Calls super constructor
        super(context, resource, textViewResourceId, strings);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.contact_layout, parent); // Represents a single row
        String[] contacts = getResources().getStringArray(R.array.main_menu);

        ImageView iv = (ImageView) row.findViewById(R.id.imageView1); // Reference to image view
        TextView tv = (TextView) row.findViewById(R.id.textView1); // Reference to text view
        ImageButton ib = (ImageButton) row.findViewById(R.id.imageButton1); // Reference to image button

        // Manipulating the stuff on a row

        tv.setText(contacts[position]);
        iv.setImageResource(R.drawable.ic_launcher);
        ib.setImageResource(R.drawable.ic_launcher);

        return row;
    }



}
}

代码没有错误。它在我关注的视频讲​​座中工作得很好。但我总是得到 UnsupportedOperationException 。请帮忙

更新:错误跟踪

09-17 08:35:00.898:E / Trace(9405):错误打开跟踪文件:没有这样的文件或目录(2) 09-17 08:35:41.187:E / AndroidRuntime(9405):致命异常:主要 09-17 08:35:41.187:E / AndroidRuntime(9405):java.lang.UnsupportedOperationException:AdapterView不支持addView(View,LayoutParams) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.widget.AdapterView.addView(AdapterView.java:477) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.LayoutInflater.inflate(LayoutInflater.java:497) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.LayoutInflater.inflate(LayoutInflater.java:396) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.LayoutInflater.inflate(LayoutInflater.java:352) 09-17 08:35:41.187:E / AndroidRuntime(9405):at com.rakeshsarangi.petrofiesta2013。联系$ MyAdapter.getView(Contacts.java:45) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.widget.AbsListView.obtainView(AbsListView.java:2299) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.widget.ListView.measureHeightOfChildren(ListView.java:1244) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.widget.ListView.onMeasure(ListView.java:1156) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.View.measure(View.java:15172) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.widget.RelativeLayout.measureChild(RelativeLayout.java:602) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.View.measure(View.java:15172) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.View.measure(View.java:15172) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.widget.LinearLayout.measureVertical(LinearLayout.java:833) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.View.measure(View.java:15172) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 09-17 08:35:41.187:E / AndroidRuntime(9405):at com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2435) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.View.measure(View.java:15172) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4214) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.Choreographer.doCallbacks(Choreographer.java:555) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.Choreographer.doFrame(Choreographer.java:525) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:711) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.os.Handler.handleCallback(Handler.java:615) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.os.Handler.dispatchMessage(Handler.java:92) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.os.Looper.loop(Looper.java:137) 09-17 08:35:41.187:E / AndroidRuntime(9405):在android.app.ActivityThread.main(ActivityThread.java:4931) 09-17 08:35:41.187:E / AndroidRuntime(9405):at java.lang.reflect.Method.invokeNative(Native Method) 09-17 08:35:41.187:E / AndroidRuntime(9405):at java.lang.reflect.Method.invoke(Method.java:511) 09-17 08:35:41.187:E / AndroidRuntime(9405):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:791) 09-17 08:35:41.187:E / AndroidRuntime(9405):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558) 09-17 08:35:41.187:E / AndroidRuntime(9405):at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

我得到了答案,感谢@Luksprog。对于那些可能有这样问题的人来说,这是一个简单的解决方案:

您无法使用此版本的inflate方法:

View row = inflater.inflate(R.layout.contactsdb, parent);

因为这会将膨胀的视图添加到父级,这在ListView中是不允许的。而是使用此版本:

View row = inflater.inflate(R.layout.contactsdb, parent, false);

会使视图膨胀但不会将其添加到父视图中。此版本很重要,因为它会为您的虚增视图提供正确的LayoutParams