Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
必须为TextView android错误提供资源ID_Android_Arrays_Resources_Textview - Fatal编程技术网

必须为TextView android错误提供资源ID

必须为TextView android错误提供资源ID,android,arrays,resources,textview,Android,Arrays,Resources,Textview,我花了一整天的时间来解决这个错误。我不明白为什么我的logcat打印: 05-06 21:45:59.559: ERROR/ConversationList(9023): We have chats... 05-06 21:45:59.609: ERROR/ConversationList(9023): UpdateList end... 05-06 21:45:59.839: ERROR/ArrayAdapter(9023): You must supply a resource ID for

我花了一整天的时间来解决这个错误。我不明白为什么我的logcat打印:

05-06 21:45:59.559: ERROR/ConversationList(9023): We have chats... 05-06 21:45:59.609: ERROR/ConversationList(9023): UpdateList end... 05-06 21:45:59.839: ERROR/ArrayAdapter(9023): You must supply a resource ID for a TextView 05-06 21:45:59.839: DEBUG/AndroidRuntime(9023): Shutting down VM 05-06 21:45:59.839: WARN/dalvikvm(9023): threadid=3: thread exiting with uncaught exception (group=0x4000fe70) 05-06 21:45:59.839: ERROR/AndroidRuntime(9023): Uncaught handler: thread main exiting due to uncaught exception 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.AbsListView.obtainView(AbsListView.java:1269) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.ListView.makeAndAddView(ListView.java:1623) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.ListView.fillDown(ListView.java:607) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.ListView.fillFromTop(ListView.java:664) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.ListView.layoutChildren(ListView.java:1481) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.AbsListView.onLayout(AbsListView.java:1113) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.view.View.layout(View.java:6133) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.LinearLayout.onLayout(LinearLayout.java:918) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.view.View.layout(View.java:6133) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.view.View.layout(View.java:6133) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.LinearLayout.onLayout(LinearLayout.java:918) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.view.View.layout(View.java:6133) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.view.View.layout(View.java:6133) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.view.ViewRoot.performTraversals(ViewRoot.java:929) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.view.ViewRoot.handleMessage(ViewRoot.java:1482) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.os.Handler.dispatchMessage(Handler.java:99) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.os.Looper.loop(Looper.java:123) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.app.ActivityThread.main(ActivityThread.java:3948) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at java.lang.reflect.Method.invokeNative(Native Method) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at java.lang.reflect.Method.invoke(Method.java:521) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at dalvik.system.NativeStart.main(Native Method) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): Caused by: java.lang.ClassCastException: android.widget.LinearLayout 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340) 05-06 21:45:59.879: ERROR/AndroidRuntime(9023): ... 30 more
我很确定问题出在你的
对话项目的布局上。必须为资源提供一个单一文本视图的状态。那个布局是什么样子的

举个例子,这就是简单的列表项目1的布局,你的应该非常相似

<?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"
/>

编辑:


我刚刚编辑了这个问题,以便显示您的布局。您的布局确实是错误的,如果您使用的是该构造函数,那么就不能有线性布局。您可以使用另一个构造函数(
ArrayAdapter(上下文上下文、int资源、int textViewResourceId、T[]对象)
)并提供您的自定义布局和TextView的id。

也有同样的问题,需要从以下位置更改自定义适配器:

  ListAdapter adapter = new ArrayAdapter<String>(
            this,
            R.layout.conversation_item,
            chatList
        );
    getListView().setAdapter(adapter);
ListAdapter=新阵列适配器(
这
R.layout.conversation_项目,
聊天列表
);
getListView().setAdapter(适配器);
致:

ListAdapter=新阵列适配器(
这
R.layout.conversation_项目,
R.id.sometextview从布局中查看,
聊天列表
);
getListView().setAdapter(适配器);

我认为所选的
TextView
将用于生成rowid,这可能解释了它需要是唯一的,但我还不确定。

每当需要在自定义布局视图中列出一组对象时,最好使用自定义适配器。参考这个简单而漂亮的参考

您使用的构造函数严格用于CheckedTextView。由于在xml文件中还提供了线性布局,因此出现了问题。最简单的解决方案是删除线性布局,只保留CheckedTextView。干杯

嗯。。这看起来很有趣。从错误中可以看出,您应该向ArrayAdapter构造函数提供TextView ID,但文档中说
textViewResourceId;包含文本视图的布局文件的资源ID,在实例化视图时使用
。您尝试过使用TextView ID吗?可能您想使用TextView“对话”项(但是,我注意到,您有两个,因此最终可能需要使用自定义的getView()方法)。新建ArrayAdapter(此,R.layout.conversation\u项,R.id.conversation\u项,聊天列表)确定我尝试ListAdapter=新建ArrayAdapter(此,R.layout.conversation\u项,R.id.conversation\u项,聊天列表);但同样的错误,我放弃了…没有人是解决方案?请查看java文件并帮助我修复android.R.layout.simple_list_item_1改为R.layout.conversation_item,hop!
ListAdapter adapter = new ArrayAdapter<String>(
        this,
        R.layout.conversation_item,
        chatList
);
getListView().setAdapter(adapter);
<?xml version="1.0" encoding="UTF-8"?>
 <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      xmlns:android="http://schemas.android.com/apk/res/android"

      android:id="@+id/conversation_item">

          <TextView 
              android:layout_height="wrap_content" 
              android:id="@+id/conversation_item_name" 
              android:text="Name Surname" 
              android:textColor="#ffffff" 
              android:textStyle="bold" 
              android:textSize="16dip" 
              android:layout_width="fill_parent" 
              android:layout_marginTop="10dip" 
              android:layout_marginBottom="10dip" 
              android:layout_marginLeft="10dip">
          </TextView>
          <TextView 
              android:layout_height="wrap_content" 
              android:id="@+id/conversation_item_date" 
              android:layout_width="fill_parent" 
              android:text="17.6.2010 15:30">
          </TextView>
          <TextView 
              android:layout_height="wrap_content"
              android:layout_width="fill_parent" 
              android:id="@+id/conversation_item_message" 
              android:text="Just some message..." 
              android:textColor="#ffffff">
          </TextView>   
</LinearLayout>
<?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"
/>
  ListAdapter adapter = new ArrayAdapter<String>(
            this,
            R.layout.conversation_item,
            chatList
        );
    getListView().setAdapter(adapter);
  ListAdapter adapter = new ArrayAdapter<String>(
            this,
            R.layout.conversation_item,
            R.id.sometextview_from_the_layout,
            chatList
        );
    getListView().setAdapter(adapter);