问题描述
- 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; }
解决方案
如果传入的id不等于0,m_pickerDialog.setTitle就报空指针了。
解决方案二:
如果id不等于0,就是跳过来case 0,那样m_pickerDialog就没有被初始化,你后面访问就会报空指针
解决方案三:
我觉得switch里应该设置个default
解决方案四:
你在switch case外面也加一个new试试看啊
时间: 2024-11-05 14:53:00