2013-07-24 43 views
0

大家好我是新手到android编程。我想通过数组适配器填充ListView。运行后错误是:不幸的是应用程序停止。我不知道什么是错的。我试着运行代码没有setAdapter和它工作正常。这里是我的代码使用ArrayAdapter填充ListView,应用程序停止

布局文件: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); 
    
    } 
    
+0

这里是日志开始:d/dalvikvm(786):GC_FOR_ALLOC释放77K,9%的游离2546K/2772K,暂停71毫秒,总77ms D/AndroidRuntime(786):关闭虚拟机 07-24 23:09:11.386:W/dalvikvm(786):threadid = 1:线程以未捕获的异常退出(group = 0x40a71930) 07-24 23:09:11.415 :E/AndroidRuntime(786):致命例外:主 07-24 23:09:11.415:E/AndroidRuntime(786):android.content.res.Resources $ NotFoundException:资源ID#0x7f080002类型#0x12无效 07-24 23:09:11.415:E/AndroidRuntime(7 86):\t at android.content.res.Resources.loadXmlResourceParser(Resources.java:2144) – user2616375

回答

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,因为它的内容视图还没有被指定。

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

在日志中,我发现无效资源ID:00079x,其中R.java指向emp_item Textview,显然文本视图是在emp_list_item中声明的.xml文件。我在activity_main文件中创建了文本视图,并在适配器中将其引用。现在应用程序没有给出任何错误,但没有任何输出。 – user2616375

+0

“没有任何输出。”你的意思是你的列表没有被填充?或者整个屏幕是空白的,甚至不显示'edittext'和按钮? –

+0

是的整个屏幕空白。在更改textview后:日志说07-24 23:33:21.545:E /跟踪(854):打开跟踪文件时出错:没有这样的文件或目录(2) – user2616375