android - 致命异常 : main java. lang.IllegalStateException:ArrayAdapter 要求资源 ID 为 TextView

标签 android

<分区>

我正在我的用户类中创建简单的 ListView,但是每次启动我的应用程序时,我都会在 logcat 中收到错误,这会给出

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

我不知道为什么在 ArrayAdapter 中会导致错误。我只是按照本教程学习

http://www.tutorialspoint.com/android/android_list_view.htm

提前谢谢你。

package com.example.myemployee;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;



public class User extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.user_layout);



        String[] countryArray = {"India", "Pakistan", "USA", "UK"};
        ArrayAdapter adapter = new ArrayAdapter<String>(this,
                R.layout.user_txtview, countryArray);

        ListView listView = (ListView) findViewById(R.id.lstview_user);
        listView.setAdapter(adapter);

    }

}

用户布局.xml

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


    <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lstview_user" android:layout_gravity="center_horizontal"/>
</LinearLayout>

user_txtview.xml

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

</TextView>

最佳答案

改变这个

ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.user_txtview, countryArray);

对此

ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listitem,R.id.textview, countryArray);

listitem.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/textview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</TextView>

documentation说:

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.

关于android - 致命异常 : main java. lang.IllegalStateException:ArrayAdapter 要求资源 ID 为 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28153885/

相关文章:

android - 使用 FFMPEG 和 JNI 压缩视频

android - 无法将从相机点击的图像上传到服务器

android - IOS转Android时React-Native Element类型无效

android - managedQuery 没有关闭游标 onDestroy?

android - 应用程序无法安装 Android 9 (Pie)

android - 进度指示器

java - 在 Android 中存储和使用 OAuth2 token 的最佳实践?

java - 如何在 recyclerview 上缓存 Parcelable 模型

java - 为什么方法参数中要用Final

android - 在android中检测抖动