0
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
    </TextView>

</LinearLayout> 

The above is my layoutm "messages.xml"

public class TabClass extends ListFragment {    

    @Override
      public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String[] values = new String[] { "label1", "label2", "label3"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            R.layout.messages, values);
        setListAdapter(adapter);
      }

      @Override
      public void onListItemClick(ListView l, View v, int position, long id) {
        // Do something with the data

      }

}

I get this error when i try to compile the application "ArrayAdapter requires the resource ID to be a TextView"

theJava
  • 14,620
  • 45
  • 131
  • 172

1 Answers1

2

Your messages.xml contains a LinearLayout with ImageView and TextView. Instead, it should just contain a TextView. See the documentation here. Your ArrayAdapter constructor's second parameter takes :

The resource ID for a layout file containing a TextView to use when instantiating views.

Hint: You might want to create a custom adapter class (which extends BaseAdapter) instead of the ArrayAdapter class.

Community
  • 1
  • 1
Dhruv Gairola
  • 9,102
  • 5
  • 39
  • 43