当选择日期超出最小或最大日期时,DatePicker会崩溃。 Android 5.1

时间:2015-08-26 16:16:49

标签: android datepicker

我有这段代码:

public void onDateSet( DatePicker view, int year, int monthOfYear, int dayOfMonth )
{
    Log.v( "DateRangePicker", "Date CHANGED" );
}

...

Calendar calender = Calendar.getInstance();
calender.setTime( new Date() );
DatePickerDialog dialog = new DatePickerDialog( 
        getActivity(), 
        this,
        calender.get( Calendar.YEAR ), calender.get( Calendar.MONTH ),
        calender.get( Calendar.DAY_OF_MONTH ) );

DatePicker picker = dialog.getDatePicker();
picker.setMaxDate( System.currentTimeMillis() );
dialog.show();

我看到post为android 4.2提供了这样的问题,似乎可以在更多版本上解决。但不是我的情况。

enter image description here

当我将月份滚动到(9月)时,它会给出下面的崩溃。我不确定,但我认为九月不应该在那里。

  FATAL EXCEPTION: main
  Process: com.hizzo.counter, PID: 23871
  java.lang.IllegalArgumentException: Time not between Mon Jan 01 00:00:00 AMT 1900 and Wed Aug 26 11:49:46 AMT 2015
        at android.widget.CalendarViewLegacyDelegate.goTo(CalendarViewLegacyDelegate.java:775)
        at android.widget.CalendarViewLegacyDelegate.setDate(CalendarViewLegacyDelegate.java:592)
        at android.widget.CalendarView.setDate(CalendarView.java:495)
        at android.widget.DatePicker$DatePickerSpinnerDelegate.updateCalendarView(DatePicker.java:1069)
        at android.widget.DatePicker$DatePickerSpinnerDelegate.access$800(DatePicker.java:536)
        at android.widget.DatePicker$DatePickerSpinnerDelegate$1.onValueChange(DatePicker.java:642)
        at android.widget.NumberPicker.notifyChange(NumberPicker.java:1888)
        at android.widget.NumberPicker.setValueInternal(NumberPicker.java:1674)
        at android.widget.NumberPicker.scrollBy(NumberPicker.java:1137)
        at android.widget.NumberPicker.computeScroll(NumberPicker.java:1091)
        at android.view.View.updateDisplayListIfDirty(View.java:14172)
        at android.view.View.getDisplayList(View.java:14207)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3391)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3370)
        at android.view.View.updateDisplayListIfDirty(View.java:14145)
        at android.view.View.getDisplayList(View.java:14207)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3391)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3370)
        at android.view.View.updateDisplayListIfDirty(View.java:14145)
        at android.view.View.getDisplayList(View.java:14207)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3391)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3370)
        at android.view.View.updateDisplayListIfDirty(View.java:14145)
        at android.view.View.getDisplayList(View.java:14207)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3391)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3370)
        at android.view.View.updateDisplayListIfDirty(View.java:14145)
        at android.view.View.getDisplayList(View.java:14207)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3391)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3370)
        at android.view.View.updateDisplayListIfDirty(View.java:14145)
        at android.view.View.getDisplayList(View.java:14207)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3391)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3370)
        at android.view.View.updateDisplayListIfDirty(View.java:14145)
        at android.view.View.getDisplayList(View.java:14207)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3391)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3370)
        at android.view.View.updateDisplayListIfDirty(View.java:14145)
        at android.view.View.getDisplayList(View.java:14207)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3391)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3370)
        at android.view.View.updateDisplayListIfDirty(View.java:14145)
        at android.view.View.getDisplayList(View.java:14207)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3391)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3370)
        at android.view.View.updateDisplayListIfDirty(View.java:14145)
        at android.view.View.getDisplayList(View.java:14207)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2560)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2382)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2012)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1084)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5990)
        at android.view.Choreographer$Call

2 个答案:

答案 0 :(得分:0)

这必须是DatePicker类本身的错误,我首先设置minDate,然后设置maxDate(按此顺序)。如果我交换顺序或省略其中一个方法调用,我会得到一个奇怪的异常。

答案 1 :(得分:0)

对于某些Android版本我遇到了同样的问题,解决方法是将MaxDate设置如下:

Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.YEAR, maxYear);
cal1.set(Calendar.HOUR_OF_DAY, 23);
cal1.set(Calendar.MINUTE, 59);
cal1.set(Calendar.SECOND, 59);
cal1.set(Calendar.MILLISECOND,999);
datePicker.setMaxDate(cal1.getTimeInMillis());

要点是将最大日期设置为最大毫秒,表示给定的日期/日(在一天结束时)。设置minDate(在当天开始时)是相反的