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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With