Home » Android » android – NullPointerException when using DatePicker setMaxDate() in a DatePickerDialog

android – NullPointerException when using DatePicker setMaxDate() in a DatePickerDialog

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am using a DatePickerDialog, and I am trying to set the minimum and maximum dates such that the user can only select a date within the previous 24 hours or next 24 hours.

Here is the code. (Note that it only tries to set the min/max range if running on Android 3.0 or higher.)

private void showDatePickerDialog() {
    DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, monthOfYear);
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            onCalendarChanged();
        }
    };

    DatePickerDialog dialog = new DatePickerDialog(this,
                                                   listener,
                                                   calendar.get(Calendar.YEAR),
                                                   calendar.get(Calendar.MONTH),
                                                   calendar.get(Calendar.DAY_OF_MONTH));
    setDatePickerDialogRange(dialog);
    dialog.show();
}

@SuppressLint("NewApi")
private void setDatePickerDialogRange(DatePickerDialog dialog) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        DatePicker picker = dialog.getDatePicker();
        long oneDayMilliseconds = 24 * 60 * 60 * 1000;
        long nowMilliseconds = new Date().getTime();
        long minDate = nowMilliseconds - oneDayMilliseconds;
        long maxDate = nowMilliseconds + oneDayMilliseconds;
        picker.setMinDate(minDate);
        picker.setMaxDate(maxDate);
    }
}

This works fine when running on a 2.2 device, but on a 4.0.4 device (Galaxy Tab 2 7.0), I get this exception:

11-14 07:58:08.917: E/AndroidRuntime(16611): FATAL EXCEPTION: main
11-14 07:58:08.917: E/AndroidRuntime(16611): java.lang.NullPointerException
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.CalendarView.onScroll(CalendarView.java:910)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.CalendarView.access$600(CalendarView.java:74)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.CalendarView$2.onScroll(CalendarView.java:771)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1391)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.ListView.layoutChildren(ListView.java:1705)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.AbsListView.onLayout(AbsListView.java:2040)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.View.layout(View.java:11330)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.ViewGroup.layout(ViewGroup.java:4235)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1644)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1502)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.View.layout(View.java:11330)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.ViewGroup.layout(ViewGroup.java:4235)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.View.layout(View.java:11330)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.ViewGroup.layout(ViewGroup.java:4235)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1644)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1633)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1417)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.View.layout(View.java:11330)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.ViewGroup.layout(ViewGroup.java:4235)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.View.layout(View.java:11330)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.ViewGroup.layout(ViewGroup.java:4235)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.View.layout(View.java:11330)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.ViewGroup.layout(ViewGroup.java:4235)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.View.layout(View.java:11330)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.ViewGroup.layout(ViewGroup.java:4235)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1644)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1502)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.View.layout(View.java:11330)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.ViewGroup.layout(ViewGroup.java:4235)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.View.layout(View.java:11330)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.ViewGroup.layout(ViewGroup.java:4235)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.View.layout(View.java:11330)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.ViewGroup.layout(ViewGroup.java:4235)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.View.layout(View.java:11330)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.ViewGroup.layout(ViewGroup.java:4235)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1548)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2505)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.os.Looper.loop(Looper.java:137)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at android.app.ActivityThread.main(ActivityThread.java:4514)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at java.lang.reflect.Method.invokeNative(Native Method)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at java.lang.reflect.Method.invoke(Method.java:511)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-14 07:58:08.917: E/AndroidRuntime(16611):    at dalvik.system.NativeStart.main(Native Method)

This happens immediately. The DatePickerDialog is never displayed.

If I comment out the setMaxDate() line, then it works fine. In this case, the minimum date is respected.

If I change it to setMaxDate(nowMilliseconds), I still get the NullPointerException.

How to&Answers:

Been working on this for quite some time. This is a bug in Android. Just turn off the Calendar View and you’ll be fine:

picker.setCalendarViewShown(false);

Answer:

Try replacing:

setDatePickerDialogRange(dialog);
dialog.show();

to:

dialog.show();
someView.post(new Runnable() {
  public void run() {
    setDatePickerDialogRange(dialog);
  }
});

(where someView is some View, or a Handler if you have one)

This will delay the call to setDatePickerDialogRange() until after the dialog has been rendered and may be a workaround for your problem.

Answer:

I have seen an issue..

DatePicker setMinDate() and setMaxDate() do not work when the new date is the same year as the old min/max date

Please refer the link below.
Bugs on DatePicker setMinDate() and setMaxDate()

Answer:

This worked >= API 19. Don’t know if works under.
Thanks @JGGerrits.

Calendar min = (Calendar) minDate.clone();
Calendar max = (Calendar) maxDate.clone();
Calendar selected = (Calendar) selectedDate.clone();
min.set(Calendar.HOUR_OF_DAY, 0);
max.set(Calendar.HOUR_OF_DAY, 23);
selected.set(Calendar.HOUR_OF_DAY, 12);

DatePickerDialog datePickerDialog =
      new DatePickerDialog(context, callBack, selected);

if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
  datePickerDialog.getDatePicker().setCalendarViewShown(false);
}

datePickerDialog.getDatePicker().setMinDate(min.getTimeInMillis());
datePickerDialog.getDatePicker().setMaxDate(max.getTimeInMillis());
datePickerDialog.show();