我正在使用DatePickerDialog,并且尝试设置最小和最大日期,以便用户只能选择前24小时或后24小时内的日期。
这是代码。 (请注意,如果在Android 3.0或更高版本上运行,它只会尝试设置最小/最大范围。)
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);
}
}
在2.2设备上运行时此方法工作正常,但在4.0.4设备(Galaxy Tab 2 7.0)上运行时,出现此异常:
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)
这会立即发生。永远不会显示DatePickerDialog。
如果我注释掉
setMaxDate()
行,那么它可以正常工作。在这种情况下,将遵守最短日期。如果将其更改为
setMaxDate(nowMilliseconds)
,我仍然会收到NullPointerException。 最佳答案
已经为此工作了一段时间。这是Android中的错误。只需关闭“日历 View ”,就可以了:
picker.setCalendarViewShown(false);
关于android - 在DatePickerDialog中使用DatePicker setMaxDate()时发生NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13379512/