java.lang.IllegalStateException: ArrayAdapter 要求资源 ID 为 TextView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23020408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:05:13  来源:igfitidea点击:

java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

javaandroid

提问by Hamamelis

I tried this tutorial http://windrealm.org/tutorials/android/android-listview.php"A Simple Android ListView Example"

我试过这个教程http://windrealm.org/tutorials/android/android-listview.php“一个简单的 Android ListView 示例”

But in my test in Eclipse I've crash in android application.

但是在我在 Eclipse 中的测试中,我在 android 应用程序中崩溃了。

In LogCat I've this:

在 LogCat 我有这个:

04-11 20:17:24.170: E/AndroidRuntime(7062): 
java.lang.IllegalStateException: ArrayAdapter requires the resource ID
to be a TextView

Why the crash ?

为什么会崩溃?

class java

类 java

private ListView mainListView;
private ArrayAdapter<String> listAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mains);

        mainListView = (ListView) findViewById(R.id.mainListView);

        String[] xRemote = Remote.split(";");

        ArrayList<String> planetList = new ArrayList<String>();

        planetList.addAll(Arrays.asList(xRemote));

        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
                planetList);

        listAdapter.addAll(xRemote);

        mainListView.setAdapter(listAdapter);

mains.xml

电源文件

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

        <ListView android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:id="@+id/mainListView">
        </ListView      
    </LinearLayout>

simplerow.xml

simplerow.xml

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

    <TextView
        android:id="@+id/rowTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="16sp" >
    </TextView>

</LinearLayout>

采纳答案by Emanuel Moecklin

Use

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
    R.id.rowTextView, planetList);

The documentationsays:

文件说:

By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

默认情况下,此类期望提供的资源 ID 引用单个 TextView。如果您想使用更复杂的布局,请使用也带有字段 id 的构造函数。该字段 id 应引用较大布局资源中的 TextView。

回答by Benoit

In simplerow.xml remove the LinearLayout and directly use TextView as the root

在 simplerow.xml 中去掉 LinearLayout 直接使用 TextView 作为根