Question

I am coding an activity where I use Custom lists. In each row of the list, I want an image view, a text view and finally an image button. Below is the xml for the layout

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

Now the code in the java file:

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;
    }



}
}

The code has no errors. It works fine in a video lecture I am following. But I always get an UnsupportedOperationException. Please help

UPDATE: The error trace

09-17 08:35:00.898: E/Trace(9405): error opening trace file: No such file or directory (2) 09-17 08:35:41.187: E/AndroidRuntime(9405): FATAL EXCEPTION: main 09-17 08:35:41.187: E/AndroidRuntime(9405): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.widget.AdapterView.addView(AdapterView.java:477) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.LayoutInflater.inflate(LayoutInflater.java:497) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 09-17 08:35:41.187: E/AndroidRuntime(9405): at com.rakeshsarangi.petrofiesta2013.Contacts$MyAdapter.getView(Contacts.java:45) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.widget.AbsListView.obtainView(AbsListView.java:2299) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.widget.ListView.onMeasure(ListView.java:1156) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.View.measure(View.java:15172) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.View.measure(View.java:15172) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.View.measure(View.java:15172) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.View.measure(View.java:15172) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 09-17 08:35:41.187: E/AndroidRuntime(9405): at 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): at android.view.View.measure(View.java:15172) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.Choreographer.doCallbacks(Choreographer.java:555) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.Choreographer.doFrame(Choreographer.java:525) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.os.Handler.handleCallback(Handler.java:615) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.os.Handler.dispatchMessage(Handler.java:92) 09-17 08:35:41.187: E/AndroidRuntime(9405): at android.os.Looper.loop(Looper.java:137) 09-17 08:35:41.187: E/AndroidRuntime(9405): at 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)

Was it helpful?

Solution

I got my answer, thanks to @Luksprog. For those who might have problems like this, here is the simple solution:

You cannot use this version of the inflate method:

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

because this will add the inflated View to the parent, which isn't allowed in a ListView. Instead use this version:

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

which will inflate the view but will not add it to the parent. This version is important because it will provide the proper LayoutParams for your inflated view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top