2014-12-03 64 views
-1

我在列表视图中收到空指针异常。当在activity_main.xml中创建列表视图时,它工作正常,但是当我在独立文件中创建listview时发生错误。列表视图的空指针异常

package pk.edu.pucit.lab_02; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     String[] username = { "hamad", "ali", "ahmed", "riaz", "hamad", "ali", 
       "ahmed", "riaz", "hamad", "ali", "ahmed", "riaz", "hamad", 
       "ali", "ahmed", "riaz", "hamad", "ali", "ahmed", "riaz", 
       "hamad", "ali", "ahmed", "riaz", "hamad", "ali", "ahmed", 
       "riaz" }; 

     ListView listView = (ListView) findViewById(R.id.listView); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(
       getApplicationContext(), R.layout.acticity_linear_layout, 
       R.id.names, username); 
     listView.setAdapter(adapter); 
    } 
} 

这是我activity_linear_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/names" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:layout_marginTop="10dp"> 
    </TextView> 

    <TextView 
     android:id="@+id/scores" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:layout_marginTop="10dp"> 
    </TextView> 
</LinearLayout> 

这是我activity_list_view.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 

     android:id="@+id/listView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     > 
</ListView> 

这是我的menifest文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="pk.edu.pucit.lab_02" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="19" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

粘贴logcat在这里,也是第二种情况下的代码,它会给你例外。 – Nazgul 2014-12-03 07:58:42

+0

你只发布了工作代码,它确实看起来很好。请张贴不工作的部分(不同文件部分)以及崩溃堆栈。 – Bruce 2014-12-03 08:00:04

+0

你的'activity_main.xml'需要包含一个ID为listView的ListView。不要把它放在单独的'activity_list_view.xml'中 – Wicked161089 2014-12-03 08:01:44

回答

0

你的问题是在这里

setContentView(R.layout.activity_main); 

但是listView是activity_list_view的一部分,因此ListView listView = (ListView) findViewById(R.id.listView);将返回空指针。

通过在主布局内移动listView来修复它。

0

R.id.listViewR.layout.activity_main

不再一部分不可用在这里

setContentView(R.layout.activity_main);

,但你在这里搜索

ListView listView = (ListView) findViewById(R.id.listView);

给你空。

0

请在您的主要xml中尝试此操作。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <TextView 
     android:id="@+id/names" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:layout_marginTop="10dp"> 
    </TextView> 
<include 
     android:id="@+id/container_header_lyt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="..." 
     android:layout_toLeftOf="..." 
     layout="@layout/activity_list_view" /> 
    <TextView 
     android:id="@+id/scores" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:layout_marginTop="10dp"> 
    </TextView> 
</LinearLayout>