You must supply a resource ID for a TextView android error

后端 未结 4 1700
清酒与你
清酒与你 2020-12-03 10:08

I try all this day to resolve this error. I don\'t understand why my logcat prints:

05-06 21:45:59.559:
ERROR/ConversationList(9023): We have
chats... 05-06 21:45:         


        
相关标签:
4条回答
  • 2020-12-03 10:58

    Had the same problem, needed to change the custom adapter from:

      ListAdapter adapter = new ArrayAdapter<String>(
                this,
                R.layout.conversation_item,
                chatList
            );
        getListView().setAdapter(adapter);
    

    to:

      ListAdapter adapter = new ArrayAdapter<String>(
                this,
                R.layout.conversation_item,
                R.id.sometextview_from_the_layout,
                chatList
            );
        getListView().setAdapter(adapter);
    

    I think the selected TextView will be used to generate the rowid, which may explain its need to be unique, but I'm not sure yet.

    0 讨论(0)
  • 2020-12-03 11:02

    I'm pretty sure the problem lies in your conversation_item layout. The docs state that you must provide a resource with a single TextView. What does that layout look like?

    As an example, this is what the simple_list_item_1 layout looks like, yours should be pretty similar.

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center_vertical"
        android:paddingLeft="6dip"
        android:minHeight="?android:attr/listPreferredItemHeight"
    />
    

    EDIT:

    I just edited the question so your layout will show. Your layout is indeed wrong, you cannot have the linear layout there if you're using that constructor. You can use the other constructor ( ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) ) and provide your custom layout and your TextView's id.

    0 讨论(0)
  • 2020-12-03 11:06

    The constructor you are using is for strictly a CheckedTextView. The problem arises due to you also providing a linear layout in your xml file. The most simple solution is to remove the linear layout and keep only the CheckedTextView. Cheers!

    0 讨论(0)
  • 2020-12-03 11:09

    It is better to use a custom adapter whenever a Array Of Objects need to be listed in a Custom Layout View. Refer this simple and beautiful reference Populating a List View with a Custom Adapter

    0 讨论(0)
提交回复
热议问题