2

我使用带有 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)

我发现了一些其他类似这样这样的帖子,告诉我一个 Android 错误。

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

提前致谢,

罗恩

更新

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

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

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

4

1 回答 1

0

我有同样的问题,适用于 Android 4.4.2 及更高版本,但在 Android 4.2.2 上出现异常似乎 Android 正在保存活动的状态,并且时间元素之一为空。我的屏幕上还有日期选择器,我可以很好地更改它。

如果在移动到新活动后关闭旧活动,则不会出现异常。如果您在顶部添加新活动,您将获得异常。

代码:

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();
于 2016-01-04T20:19:25.827 回答