java.lang.IllegalStateException:ArrayAdapter 要求资源 ID 为 TextView [重复]

2024-01-07

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

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

在 LogCat 我有这个:

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

为何坠机?

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);

电源.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

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

Use

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

The 文档 http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter%28android.content.Context,%20int,%20int,%20T%5B%5D%29 says:

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java.lang.IllegalStateException:ArrayAdapter 要求资源 ID 为 TextView [重复] 的相关文章

随机推荐