gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:54:34 28 4
gpt4 key购买 nike

<分区>

我正在我的用户类中创建简单的 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>

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com