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