使用ArrayAdapter填充ListView,应用程序已停止

时间:2013-07-24 21:56:47

标签: 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.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>
  1. Main_activity.java

    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); 
    
    }
    

2 个答案:

答案 0 :(得分:2)

您需要先设置内容视图,如下面的代码所示。

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_list)上返回null,因为它的内容视图尚未指定。

答案 1 :(得分:0)

必须先使用

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);
}