我在列表视图中遇到空指针异常。当在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>
这是我的清单文件
<?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 :(得分:0)
你的问题在这里
setContentView(R.layout.activity_main);
但listView是activity_list_view
的一部分,因此ListView listView = (ListView) findViewById(R.id.listView);
将返回空指针。
通过在主要布局中移动listView
来修复它。
答案 1 :(得分:0)
R.id.listView
已不再是R.layout.activity_main
这里没有
setContentView(R.layout.activity_main);
但你在这里搜索
ListView listView = (ListView) findViewById(R.id.listView);
给你null。
答案 2 :(得分: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>