0

当我开始我的 CalendarPopUp 活动时,我得到一个NullPointerException.

这是我的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setUpWindow();
    Intent intent = getIntent();
    Bundle extras = intent.getExtras(); // This is Line 37
    boolean isForResult = extras.containsKey("result");
            //...
}

这是例外

07-30 09:33:47.269: E/AndroidRuntime(19979): FATAL EXCEPTION: main
07-30 09:33:47.269: E/AndroidRuntime(19979): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.moveinblue.planner/com.moveinblue.planner.utils.popup.CalendarPopup}: java.lang.NullPointerException
07-30 09:33:47.269: E/AndroidRuntime(19979):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at android.app.ActivityThread.access$600(ActivityThread.java:132)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at android.os.Looper.loop(Looper.java:137)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at android.app.ActivityThread.main(ActivityThread.java:4575)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at java.lang.reflect.Method.invoke(Method.java:511)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at dalvik.system.NativeStart.main(Native Method)
07-30 09:33:47.269: E/AndroidRuntime(19979): Caused by: java.lang.NullPointerException
07-30 09:33:47.269: E/AndroidRuntime(19979):    at com.moveinblue.planner.utils.popup.CalendarPopup.onCreate(CalendarPopup.java:37)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at android.app.Activity.performCreate(Activity.java:4465)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-30 09:33:47.269: E/AndroidRuntime(19979):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
07-30 09:33:47.269: E/AndroidRuntime(19979):    ... 11 more

有人可以在这里帮助我吗?

调试后我知道意图的 mExtras 对象为空(意图实际上不为空,但它的大部分内部对象都是)。当然调用的getExtras()时候返回null,调用containsKey()的时候会抛出一个NullPointerException.

4

1 回答 1

2

getExtras()如果没有向意图(文档)添加额外内容,则该方法返回 null。尝试extra先将其添加到意图中,然后启动CalendarPopUpActivity,然后BundleIntent.

编辑:

如果您并不总是需要额外的,请先像这样检查

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setUpWindow();
    Intent intent = getIntent();
    Bundle extras = intent.getExtras(); // This is Line 37
    if (extras != null) {
        boolean isForResult = extras.containsKey("result");
        //...
    }
}

这样,您将能够检测到意图中是否有额外内容,并且您可以从NullPointerException

于 2012-07-30T07:47:45.773 回答