如何解决ArrayAdapter requires the resource ID to be a TextView

今天碰到这样一个难题,搞了很久都没搞定,到网上搜索了一下,发现有很多人都在问这样的问题,现在将自己解决和分析的结果放置如下:

在ArrayAdapter()这个类中有多个构造方法,我仅据此列以作代表:

ArrayAdapter(Context context ,int textViewResourceId ,List<T> objects)

参数:

context    ----> the current context
textViewResourceId ----> the resource ID for a layout file contain a TextView to use when instantiating views.
List<T> objects ---> the objects to represent in the ListView

第一个参数:上下文,就是当前的Activity.

写法: MainActivity.this      this

第二个参数:Android sdk中自己内置的一个布局,它里面只有一个TextView,这个参数是表明我们数组中每一条数据的布局是这个View,就是将每一条数据都显示在这个View上面,也就是当装载在这个构造函数中的layout时,其layout的ID
必须是一个TextView
,简言之,第2个参数应该是ListView中每个选择项的样式,可以使用系统自带的
android.R.layout.xxx,也可以是自定义的,仅包含TextView。

创建ArrayAdapter时候必须指定一个resource,该参数决定每个列表项的外观样式

simple_list_item_1:
每个列表项都是一个普通的TextView

simple_list_item_2: 有两个TextView,一个单独在一行,就是分两行显示

simple_list_item_checked:
每个列表项都是一个已勾选的列表项

第三个参数:就是我们要显示的数据。listView会根据这三个参数,遍历AdapterData里面的每一条数据,读出一条,显示到第二个参数对应的布局中,这样就形成了我们看到的ListView.

下面是Android sdk自置的布局

路径:C:\android-sdk_r22.3-windows\android-sdk-windows\platforms\android-16\data\res\layout



simple_list_item_1.xml


<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>


simple_list_item_2.xml


<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:mode="twoLine"
>
    
    <TextView android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    android:layout_marginLeft="?android:attr/listPreferredItemPaddingLeft"
    android:layout_marginTop="8dip"
        android:textAppearance="?android:attr/textAppearanceListItem"
    />
        
    <TextView android:id="@android:id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/text1"
    android:layout_alignLeft="@android:id/text1"
        android:textAppearance="?android:attr/textAppearanceSmall"
    />

</TwoLineListItem>


simple_list_item_checked.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/textCheckMark"
    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight"
/>

*****************************************************************************************************

因为根节点必须是TextView,不然就会抛“ArrayAdapter requires the resource ID to be a TextView”

*****************************************************************************************************

时间: 2024-07-30 18:31:17

如何解决ArrayAdapter requires the resource ID to be a TextView的相关文章

ArrayAdapter requires the resource ID to be a TextView

在测试Spinner的时候,使用ArrayAdapter作为构造器,运行的时候报错,如上图: 代码: List<String> list = new ArrayList<String>(); list.add("张三"); list.add("lisi"); list.add("wangwu"); list.add("张三"); ArrayAdapter adapter = new ArrayAdapt

android编程常见问题- Resource ID #0x7f070001 type #0x12 is not valid

问题说明: AndroidRuntime(1705): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activitytest/com.example.activitytest.SecondActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f070001 type #0x12 is n

android.content.res.Resources$NotFoundException: Resource ID解决办法

android.content.res.Resources$NotFoundException: Resource ID #0x**********问题“ 解决办法: 把AndroidManifest.xml里面的版本修改一下(如果原来没有指定,就添加一下),问题就可以解决了 原因:原来的那个工程占用现在的这个虚拟机,不能正常加载现在的这个工程. android.content.res.Resources$NotFoundException: Resource ID解决办法

解决:Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2

在运行Android app时出现以下错误: --------- beginning of crash 11-28 07:53:56.485 6712-6712/com.trojane.android.learnactivity E/AndroidRuntime: FATAL EXCEPTION: main Process: com.trojane.android.learnactivity, PID: 6712 java.lang.RuntimeException: Unable to sta

android.content.res.Resources$NotFoundException: String resource ID #0x11

在练习数据绑定时,出现以下错误 FATAL EXCEPTION: main                                                                               Process: com.vogella.android.databinding, PID: 8332                                                                               andr

异常处理android.content.res.Resources$NotFoundException: String resource ID #0x44

错误:android.content.res.Resources$NotFoundException: String resource ID #0x1 原因:一般发生在参数 int resId 错误,你把String赋值给int的resId,所以编译器找不到正确的resource于是报错. 最简单的例子,检查一下你的Toast.makeText()啊textView.setText啊之类的函数,这种函数通常有几个重载,如: textView.setText(CharSequence text);

Android Resources$NotFoundException: String resource ID #0x1

今天写一个新界面,需要从其他界面通过intent传来参数,就先拿到之后通过TextView的setText显示来看看传参是否正确,写法:TextView.setText(arg) (arg是int型),然后就玩命报错  "android.content.res.Resources$NotFoundException: String resource ID #0x1",一直不知道怎么回事,看过API才明白当调用setText()方法时如果传入int型是不会被当成内容而是resourceI

android.content.res.Resources$NotFoundException: String resource ID #0x1

之前忘了记录这个错误,今天又遇到了.唉,人不能纵容自己犯懒,遂记录之. 错误:android.content.res.Resources$NotFoundException: String resource ID #0x1 原因:一般发生在参数 int resId 错误,你把String赋值给int的resId,所以编译器找不到正确的resource于是报错. 最简单的例子,检查一下你的Toast.makeText()啊textView.setText啊之类的函数,这种函数通常有几个重载,如:

android.content.res.Resources$NotFoundException: String resource ID XXXX

错误:android.content.res.Resources$NotFoundException: String resource ID XXXX 原因:一般发生在参数 int resId 错误,你把String赋值给int的resId,所以编译器找不到正确的resource于是报错. 最简单的例子,检查一下你的Toast.makeText()啊textView.setText啊之类的函数,这种函数通常有几个重载,如: textView.setText(CharSequence text);