2

I use a Samsung Galaxy S4 active with Android 4.3.

I use a TimePickerDialog which I create like this:

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

Here's the listener:

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;
        }
    }
};

Everything works like a charm UNTIL I leave the activty. Then I get the following 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)

I found a couple of other posts like this or this telling me about an Android bug.

But still: What do I do about it? I need the time picker and the NPE is not acceptable, right?

UPDATE

At line 470 I find this in the sources:

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

But these are the sources of SDK19, my phone uses Samsung branded Android 4.3...

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Ron
  • 22,128
  • 31
  • 108
  • 206

1 Answers1

0

I had the same issue, works fine for Android 4.4.2 and above but get the exception on Android 4.2.2 It seems like Android is saving the state of the activity and one of the elements of the time is null. I have on my screen also date picker and I can change it fine.

If you close the old activity after moving to the new activity, you won't get the exception. If you add the new activity on top, you will get the exception.

Code:

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
        }
    });

Also, try not to use: showDialog(X); to start the picker, use instead:

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