Sunil
Sunil

Reputation: 521

null pointer exception when using TimePickerDialog

I am working on activity which uses timepickerdialog for setting a time. But after setting up the time fom the dialog box, and then when i proceed, i get exception

as:

java.lang.RuntimeException: Unable to pause activity
Caused by: java.lang.NullPointerException
at android.widget.TimePicker.updateInputState(TimePicker.java:995)
at android.widget.TimePicker.onSaveInstanceState(TimePicker.java:640)
at android.view.View.dispatchSaveInstanceState(View.java:13561)

My code uses :

  1. OnClick

    showDialog(END_TIME_PICKER_ID);
    
  2. protected void onPrepareDialog(int id, Dialog dialog)

      case END_TIME_PICKER_ID :
            endHourOfDay = mEndCalendar.get(Calendar.HOUR_OF_DAY);
             endMinuteOfDay = mEndCalendar.get(Calendar.MINUTE);
    
            ((TimePickerDialog) dialog).updateTime(endHourOfDay, endMinuteOfDay);
            ((TimePickerDialog) dialog).show();
            break;
    
  3. protected Dialog onCreateDialog(int id)

     case END_TIME_PICKER_ID :
      return new TimePickerDialog(this, mEndTimeSetListener, endHourOfDay, endMinuteOfDay, false);
    
  4. on Time Set listener

     private TimePickerDialog.OnTimeSetListener mEndTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int hourOfDay, int minute)
    {
        endHourOfDay = hourOfDay;
        endMinuteOfDay = minute;
     GregorianCalendar endCalendar = (GregorianCalendar) mEndCalendar.clone();
        endCalendar.set(Calendar.DAY_OF_MONTH, mStartCalendar.get(Calendar.DAY_OF_MONTH));
        endCalendar.set(Calendar.HOUR_OF_DAY, endHourOfDay);
        endCalendar.set(Calendar.MINUTE, endMinuteOfDay);
        endCalendar.set(Calendar.SECOND, second);
     }
    

So when i run the above snippet, and after setting the timepicker and then when i click playbutton to go to the next activity, it gives this application crash. Then it go and plays ( new activity)

When i click back key , i get another error.

     java.lang.RuntimeException: Unable to start activity ComponentInfo

When i click ok, it starts from the activity group again with the default activity inside the activity group.

I feel all these issues are caused by the timepicker dialog. If i dont open the timepickerdialog ( to edit time), everything works fine .

Please let me know if i am doing something wrong with the timepickerdialog code.

Upvotes: 1

Views: 1366

Answers (2)

Wojciech Karbowski
Wojciech Karbowski

Reputation: 9

I have faced the same issue on android 4.4, on old version it wasnt happend. Genertally i found that this issue is connected with timePicker.

From Logcat:

E/AndroidRuntime( 8671): java.lang.NullPointerException

E/AndroidRuntime( 8671):    at android.widget. (TimePicker.java:995)

E/AndroidRuntime( 8671):    at android.widget.TimePicker.onSaveInstanceState(TimePicker.java:640)

E/AndroidRuntime( 8671):    at android.view.View.dispatchSaveInstanceState(View.java:13570)

E/AndroidRuntime( 8671):    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2813)

E/AndroidRuntime( 8671):    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2819)

E/AndroidRuntime( 8671):    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2819)

E/AndroidRuntime( 8671):    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2819)

E/AndroidRuntime( 8671):    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2819)

E/AndroidRuntime( 8671):    at android.view.View.saveHierarchyState(View.java:13553)

E/AndroidRuntime( 8671):    at com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1875)

E/AndroidRuntime( 8671):    at android.app.Dialog.onSaveInstanceState(Dialog.java:408)

E/AndroidRuntime( 8671):    at android.app.TimePickerDialog.onSaveInstanceState(TimePickerDialog.java:238)

E/AndroidRuntime( 8671):    at android.app.Activity.saveManagedDialogs(Activity.java:1267)

E/AndroidRuntime( 8671):    at android.app.Activity.performSaveInstanceState(Activity.java:1185)

E/AndroidRuntime( 8671):    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1241)

E/AndroidRuntime( 8671):    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3331)

E/AndroidRuntime( 8671):    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3390)

E/AndroidRuntime( 8671):    at android.app.ActivityThread.access$1100(ActivityThread.java:163)

E/AndroidRuntime( 8671):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1279)

E/AndroidRuntime( 8671):    at android.os.Handler.dispatchMessage(Handler.java:102)

E/AndroidRuntime( 8671):    at android.os.Looper.loop(Looper.java:157)

E/AndroidRuntime( 8671):    at android.app.ActivityThread.main(ActivityThread.java:5335)

E/AndroidRuntime( 8671):    at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime( 8671):    at java.lang.reflect.Method.invoke(Method.java:515)

E/AndroidRuntime( 8671):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)

E/AndroidRuntime( 8671):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)

E/AndroidRuntime( 8671):    at dalvik.system.NativeStart.main(Native Method)

I solved this issue by adding

<TimePicker android:id="@+id/timePicker"     
     android:layout_width="wrap_content"    
     android:layout_height="wrap_content"    
     android:layout_gravity="center_horizontal"
android:descendantFocusability="blocksDescendants">  
 <requestFocus />  
</TimePicker>  

i have also set timePicker in this order find, set24 then listener:

timePicker = (TimePicker) viewContent.findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
timePicker.setOnTimeChangedListener(listener);

This is some kind of system issue. This fix is working if you will create timepicker in xml -i believe this is also source of the problem.

Upvotes: 0

Null pointer occurs when your using variable or object without initializing it or your trying to access variable or object before declaring or initializing it

use logcat to get error after getting error in logcat go to that error by double clicking on it then you can get that object/variable which you are using before initialising /declaring it

you need to declare/initialze that object before it actualy using it

Upvotes: -1

Related Questions