2 z dong dong Z_dong_dong 于 2015.06.10 11:16 提问

oncreateDialog时报了空指针异常,求大神指导一下,我也找不到原因所在

报这个异常,我也搜了相关资料,很多人代码里最后default也返回null,为什么我报错呀?求大神知道,代码在最下面
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2378)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2430)
at android.app.ActivityThread.access$600(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5400)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.netschool.mock.MockListActivity.onCreateDialog(Unknown Source)
at android.app.Activity.onCreateDialog(Activity.java:3089)
at android.app.Activity.createDialog(Activity.java:1072)
at android.app.Activity.restoreManagedDialogs(Activity.java:1061)
at android.app.Activity.performRestoreInstanceState(Activity.java:1007)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1202)
at com.lbe.security.service.core.client.internal.InstrumentationDelegate.callActivityOnRestoreInstanceState(InstrumentationDelegate.java:130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356)
... 11 more

代码
 @Override
    protected Dialog onCreateDialog(int id) {
        m_pickerDialog = null;
        switch (id) {
        case 0:

            m_pickerDialog = new DatePickerDialog_WithoutDate(this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    Message msg = Message.obtain();
                    msg.arg1 = year;
                    msg.arg2 = monthOfYear + 1;
                    msg.what = MsgWhat.MSG_ANSW_RECO_ACTI_15;
                    m_handlerPostExecute.sendMessage(msg);

                }
            }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
            break;
        }
        m_pickerDialog.setTitle(calendar.get(Calendar.YEAR) + " 年 " + (calendar.get(Calendar.MONTH) + 1)
                + " 月");
        return m_pickerDialog;
    }

4个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.06.10 11:26
 如果传入的id不等于0,m_pickerDialog.setTitle就报空指针了。
danielinbiti
danielinbiti 回复Z_dong_dong: 网上肯定不是这么写的,如果支持,肯定也是m_pickerDialog.setTitle(calendar.get(Calendar.YEAR) + " 年 " + (calendar.get(Calendar.MONTH) + 1) + " 月");这句放到break前面进行的。
2 年多之前 回复
Z_dong_dong
Z_dong_dong 我的应用里面只穿了id为0,但是我百度别人的代码都是的,他们可以返回为null
2 年多之前 回复
bdmh
bdmh   Ds   Rxr 2015.06.10 11:28

如果id不等于0,就是跳过来case 0,那样m_pickerDialog就没有被初始化,你后面访问就会报空指针

u011240877
u011240877   2015.06.11 18:04

我觉得switch里应该设置个default

vdsvfdsbfdgf
vdsvfdsbfdgf   2015.06.26 16:56

你在switch case外面也加一个new试试看啊

Csdn user default icon
上传中...
上传图片
插入图片