Android 使用ArrayAdapter填充ListView,应用程序已停止
大家好,我是android编程的新手。我正试图通过阵列适配器填充ListView。运行后的错误是:不幸的是,应用程序停止了。我不知道出了什么问题。我尝试在没有setAdapter的情况下运行代码,结果很好。这是我的密码 布局文件: 1.activity_main.xmlAndroid 使用ArrayAdapter填充ListView,应用程序已停止,android,listview,android-arrayadapter,Android,Listview,Android Arrayadapter,大家好,我是android编程的新手。我正试图通过阵列适配器填充ListView。运行后的错误是:不幸的是,应用程序停止了。我不知道出了什么问题。我尝试在没有setAdapter的情况下运行代码,结果很好。这是我的密码 布局文件: 1.activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.co
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText android:id="@+id/emp_name"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/emp_name" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_search"
android:clickable="true"
android:onClick="search" />
</LinearLayout>
<ListView
android:id="@+id/emp_list"
android:layout_width="75dp"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
2.emp_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/emp_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TextView>
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String[] employees = {"Christophe Coenraets", "John Smith"};
ListAdapter adapter = new ArrayAdapter<String> (this,R.layout.activity_main,R.id.emp_item,employees);
ListView employeeList = (ListView) findViewById(R.id.emp_list);
employeeList.setAdapter(adapter);
setContentView(R.layout.activity_main);
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
字符串[]employees={“Christophe Coenraets”,“John Smith”};
ListAdapter=new ArrayAdapter(此,R.layout.activity\u main,R.id.emp\u项,员工);
ListView employeeList=(ListView)findViewById(R.id.emp_列表);
employeeList.setAdapter(适配器);
setContentView(R.layout.activity_main);
}
您需要首先设置内容视图,如以下代码所示
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] employees = {"Christophe Coenraets", "John Smith"};
ListAdapter adapter = new ArrayAdapter(this,R.layout.activity_main,R.id.emp_item,employees);
ListView employeeList = (ListView) findViewById(R.id.emp_list);
employeeList.setAdapter(adapter);
}
在您的情况下,它在findViewById(R.id.emp_列表)上返回null,因为它的内容视图尚未指定。必须首先使用setContentView
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] employees = {"Christophe Coenraets", "John Smith"};
ListAdapter adapter = new ArrayAdapter(this,R.layout.activity_main,R.id.emp_item,employees);
ListView employeeList = (ListView) findViewById(R.id.emp_list);
employeeList.setAdapter(adapter);
}
以下是日志的开始:D/dalvikvm(786):GC_FOR_ALLOC freed 77K,9%free 2546K/2772K,暂停71ms,总计77ms D/AndroidRuntime(786):关闭VM 07-24 23:09:11.386:W/dalvikvm(786):threadid=1:线程退出,但未捕获异常(组=0x40a71930)07-24 23:09:11.415:E/AndroidRuntime(786):致命异常:main 07-24 23:09:11.415:E/AndroidRuntime(786):android.content.res.Resources$NotFoundException:Resource ID#0x7f080002 type#0x12无效07-24 23:09:11.415:E/AndroidRuntime(786):在android.content.res.Resources.loadXmlResourceParser(Resources.java:2144)在日志中,我发现无效的资源ID:00079x,它在R.java中指向emp_item Textview,显然文本视图是在emp_list_item.xml文件中声明的。我在activity_主文件中创建了文本视图,并在adapter中引用了它。现在应用程序没有给出任何错误,但它没有任何输出。“没有任何输出。”你是说你的列表没有被填充?或者整个屏幕是空白的,甚至没有显示您的
edittext
和按钮?是的,整个屏幕是空白的。更改文本视图后:日志显示07-24 23:33:21.545:E/Trace(854):打开跟踪文件时出错:没有这样的文件或目录(2)