从Intent中检索Extras时的奇怪NPE

时间:2012-07-30 07:38:39

标签: android android-intent nullpointerexception bundle

当我启动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对象是null(intent实际上不是null,但是它的大多数内部对象都是)。当然,在调用getExtras()时,它会返回null,而在调用containsKey()时,它会抛出NullPointerException

1 个答案:

答案 0 :(得分:2)

如果没有为intent(documentation添加额外内容),getExtras()方法返回null。尝试先将extra添加到意图中,然后启动CalendarPopUpActivity,然后从Bundle中提取此Intent

编辑:

如果您不总是需要额外的,请先按照

进行检查
@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

中保存的