我正在尝试使用以下代码使用字符串数组中的值填充一个简单的列表视图,它会抛出一个空指针异常。该列表是从前一个ctivity获取的,stats this和list是有效的我已检查过它包含字符串值
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
final ListView listview = (ListView) findViewById(R.id.listview);
List<Map<String, String>> deptList = new ArrayList<Map<String,String>>();
if (extras != null) {
deplist = extras.getStringArray("deparments");
Log.w("Got","List");
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Home.this,
android.R.layout.simple_list_item_1, android.R.id.text1, deplist);
listview.setAdapter(adapter);
setContentView(R.layout.home);
这是堆栈跟踪
10-06 02:39:33.002: D/dalvikvm(1409): GC_FOR_ALLOC freed 59K, 7% free 2541K/2720K, paused 139ms, total 141ms
10-06 02:39:33.102: I/dalvikvm-heap(1409): Grow heap (frag case) to 4.058MB for 1536016-byte allocation
10-06 02:39:33.152: D/dalvikvm(1409): GC_FOR_ALLOC freed <1K, 5% free 4041K/4224K, paused 48ms, total 48ms
10-06 02:39:33.642: D/gralloc_goldfish(1409): Emulator without GPU emulation detected.
10-06 02:39:35.952: W/Got(1409): List
10-06 02:39:36.092: D/AndroidRuntime(1409): Shutting down VM
10-06 02:39:36.092: W/dalvikvm(1409): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-06 02:39:36.372: E/AndroidRuntime(1409): FATAL EXCEPTION: main
10-06 02:39:36.372: E/AndroidRuntime(1409): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usa/com.example.usa.Home}: java.lang.NullPointerException
10-06 02:39:36.372: E/AndroidRuntime(1409): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-06 02:39:36.372: E/AndroidRuntime(1409): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-06 02:39:36.372: E/AndroidRuntime(1409): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-06 02:39:36.372: E/AndroidRuntime(1409): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-06 02:39:36.372: E/AndroidRuntime(1409): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 02:39:36.372: E/AndroidRuntime(1409): at android.os.Looper.loop(Looper.java:137)
10-06 02:39:36.372: E/AndroidRuntime(1409): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-06 02:39:36.372: E/AndroidRuntime(1409): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 02:39:36.372: E/AndroidRuntime(1409): at java.lang.reflect.Method.invoke(Method.java:525)
10-06 02:39:36.372: E/AndroidRuntime(1409): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-06 02:39:36.372: E/AndroidRuntime(1409): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-06 02:39:36.372: E/AndroidRuntime(1409): at dalvik.system.NativeStart.main(Native Method)
10-06 02:39:36.372: E/AndroidRuntime(1409): Caused by: java.lang.NullPointerException
10-06 02:39:36.372: E/AndroidRuntime(1409): at com.example.usa.Home.onCreate(Home.java:51)
10-06 02:39:36.372: E/AndroidRuntime(1409): at android.app.Activity.performCreate(Activity.java:5133)
10-06 02:39:36.372: E/AndroidRuntime(1409): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-06 02:39:36.372: E/AndroidRuntime(1409): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-06 02:39:36.372: E/AndroidRuntime(1409): ... 11 more
答案 0 :(得分:2)
您正在致电
final ListView listview = (ListView) findViewById(R.id.listview);
之前
setContentView(R.layout.home);
哪个没有意义。 findViewById()
在当前虚增的视图中查找视图。但是,在{10}之后使用setContentView()
,您还没有夸大视图。将setContentView()
放在onCreate()
的开头。