离开活动时导致NullPointer的TimePicker对话框

时间:2014-05-27 13:04:36

标签: android nullpointerexception android-4.3-jelly-bean android-timepicker

我在Android 4.3上使用三星Galaxy S4。

我使用我创建的TimePickerDialog

new TimePickerDialog(this, mTimeSetListener, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true);

以下是听众:

private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        if(isStartTimeModified){
            updateStartTimeDisplay(hourOfDay, minute);
            isStartTimeModified = false;
        }else if(isEndTimeModified){
            updateEndTimeDisplay(hourOfDay, minute);
            isEndTimeModified = false;
        }
    }
};

一切都像魅力一样直到我离开这个活动。然后我得到以下NullPointerException

E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at android.widget.TimePicker.onSaveInstanceState(TimePicker.java:470)
        at android.view.View.dispatchSaveInstanceState(View.java:12623)
        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2671)
        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2677)
        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2677)
        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2677)
        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2677)
        at android.view.View.saveHierarchyState(View.java:12606)
        at com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1852)
        at android.app.Dialog.onSaveInstanceState(Dialog.java:402)
        at android.app.TimePickerDialog.onSaveInstanceState(TimePickerDialog.java:216)
        at android.app.Activity.saveManagedDialogs(Activity.java:1264)
        at android.app.Activity.performSaveInstanceState(Activity.java:1182)
        at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1233)
        at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3216)
        at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3275)
        at android.app.ActivityThread.access$1000(ActivityThread.java:150)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5279)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)

我发现了其他一些帖子,例如thisthis,告诉我有关Android的错误。

但仍然:我该怎么办呢?我需要时间选择器而NPE是不可接受的,对吗?

提前致谢,

罗恩

更新

在第470行,我在消息来源中找到了这个:

mHourSpinner.setValue(currentHour);
    if (notifyTimeChanged) { //line 470
        onTimeChanged();
    }

但这些是SDK19的来源,我的手机使用的是三星品牌的Android 4.3 ......

1 个答案:

答案 0 :(得分:0)

我有同样的问题,适用于Android 4.4.2及更高版本但在Android 4.2.2上获得例外 似乎Android正在保存活动的状态,并且时间的一个元素为null。我在屏幕上也有日期选择器,我可以改变它。

如果您在转移到新活动后关闭旧活动,则不会获得例外。如果您在顶部添加新活动,您将获得例外。

代码:

mainMenuButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(AbstractEditRecordActivity.this, AndroidPET.class);
            startActivity(intent);
            finish(); //Without finish we get exception if time picker changed
        }
    });

另外,尽量不要使用:showDialog(X);要启动选择器,请改为使用:

TimePickerDialog timePicker = mDateTimeButtons.newTimePickerDialog(getContext());
            timePicker.show();