致命异常:引起:java.lang.NullPointerException

时间:2014-08-07 12:34:33

标签: android nullpointerexception

我为下面的代码获取了这个空指针异常。

我正在尝试使用之前选择的值设置列表视图。

请帮忙。

代码:

adapter = new ArrayAdapter<String>(Brand_list.this,
            android.R.layout.simple_list_item_multiple_choice,Brand_List_Temp);

    adapter.notifyDataSetChanged();

    listView.setAdapter(adapter);

    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    Log.i("Adapter set", "Yes");
    if(checkedlistprev.isEmpty()==false){

        for(int j=0;j<checkedlistprev.size();j++){
            int pos=adapter.getPosition(checkedlistprev.get(j));
            listView.setSelection(pos);
        }

    }

错误日志:

08-07 17:52:20.674: E/AndroidRuntime(13012): FATAL EXCEPTION: main
08-07 17:52:20.674: E/AndroidRuntime(13012): Process: test.dsi.XXXXX, PID: 13012
08-07 17:52:20.674: E/AndroidRuntime(13012): java.lang.RuntimeException: Unable to start activity ComponentInfo{test.dsi.decathnavigation/test.dsi.XXXXX.Brand_list}: java.lang.NullPointerException
08-07 17:52:20.674: E/AndroidRuntime(13012):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at android.os.Looper.loop(Looper.java:136)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at android.app.ActivityThread.main(ActivityThread.java:5086)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at java.lang.reflect.Method.invokeNative(Native Method)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at java.lang.reflect.Method.invoke(Method.java:515)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at dalvik.system.NativeStart.main(Native Method)
08-07 17:52:20.674: E/AndroidRuntime(13012): Caused by: java.lang.NullPointerException
08-07 17:52:20.674: E/AndroidRuntime(13012):    at test.dsi.decathnavigation.Brand_list.onCreate(Brand_list.java:136)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at android.app.Activity.performCreate(Activity.java:5248)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
08-07 17:52:20.674: E/AndroidRuntime(13012):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
08-07 17:52:20.674: E/AndroidRuntime(13012):    ... 11 more

1 个答案:

答案 0 :(得分:0)

你不应该在for循环之前检查它是否为空..但是你应该检查以确保它不为空

if(checkedlistprev==null){

       checkedlistprev = new ArrayList<int>();// or what ever it is

}

for(int j=0;j<checkedlistprev.size();j++){
            int pos=adapter.getPosition(checkedlistprev.get(j));
            listView.setSelection(pos);
}