【问题标题】:java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView [duplicate]java.lang.IllegalStateException:ArrayAdapter 要求资源 ID 是 TextView [重复]
【发布时间】:2023-03-30 08:50:01
【问题描述】:

我试过这个教程http://windrealm.org/tutorials/android/android-listview.php“A Simple Android ListView Example”

但是在我的 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);

ma​​ins.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>

【问题讨论】:

    标签: java android


    【解决方案1】:

    使用

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

    documentation 说:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2016-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-12
        • 1970-01-01
        • 2012-03-06
        相关资源
        最近更新 更多