8

我正在使用 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。

4

4 回答 4

13

已经为此工作了一段时间。这是Android中的一个错误。只需关闭日历视图就可以了:

picker.setCalendarViewShown(false);
于 2013-09-09T14:15:15.307 回答
0

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.

于 2012-11-14T13:34:50.270 回答
0
I have seen an issue..

DatePickersetMinDate()并且setMaxDate()当新日期与旧的最小/最大日期相同时不起作用

请参考以下链接。 DatePicker setMinDate() 和 setMaxDate() 上的错误

于 2014-02-03T14:59:07.157 回答
0

这有效> = API 19。不知道是否有效。谢谢@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();
于 2017-05-31T19:47:43.087 回答