0

大家好,我是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); 
    
    }
    
4

2 回答 2

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 ,因为尚未指定其内容视图。

于 2013-07-24T22:05:45.340 回答
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);
}
于 2013-07-24T22:11:06.267 回答