我用于活动的日期选择器在KitKat上崩溃,但适用于所有其他较新的操作系统。这是我在旧设备上的例外:
java.lang.IllegalArgumentException:fromDate:Mon Apr 10 07:59:25 EDT 2017年不在日期之前:2017年4月10日星期五07:59:25
代码块堆栈跟踪指向:
private void showDatePicker(){
DatePickerDialog datePickerDialog = new DatePickerDialog(
getActivity(), this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
//this is where the crash happens
datePickerDialog.getDatePicker().setMinDate(new Date().getTime());
datePickerDialog.show();
}
如果有关该问题的信息充足,请告诉我。对此有何解决方法?
答案 0 :(得分:2)
我解决了这个问题,应用延迟:
private void showDatePicker(){
DatePickerDialog datePickerDialog = new DatePickerDialog(
getActivity(), this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
//this is where the crash happens
datePickerDialog.getDatePicker().setMinDate(new Date().getTime() - 10000);
datePickerDialog.show();
}
答案 1 :(得分:0)
要解决此问题,请在实例化DatePickerDialog之前为minDate创建一个时间戳:
Australia/Lord_Howe
由于API21之前的CalendarView和API21的private void showDatePicker(){
long now = System.currentTimeMillis();
DatePickerDialog datePickerDialog = new DatePickerDialog(
getActivity(), this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.getDatePicker().setMinDate(now);
datePickerDialog.show();
}
中的错误,发生了此崩溃。
尽管calendarViewMode == MODE_HOLO
包含以注释突出显示的更正:
setMinDate
public void setMinDate(long minDate) {
...
mMinDate.setTimeInMillis(minDate);
// make sure the current date is not earlier than
// the new min date since the latter is used for
// calculating the indices in the adapter thus
// avoiding out of bounds error
Calendar date = mAdapter.mSelectedDate;
if (date.before(mMinDate)) {
mAdapter.setSelectedDay(mMinDate);
}
// reinitialize the adapter since its range depends on min date
mAdapter.init();
中的支票仅将setSelectedDay
和mMinDate
进行日期精度比较:
mSelectedDate
public void setSelectedDay(Calendar selectedDay) {
if (selectedDay.get(Calendar.DAY_OF_YEAR) == mSelectedDate.get(Calendar.DAY_OF_YEAR)
&& selectedDay.get(Calendar.YEAR) == mSelectedDate.get(Calendar.YEAR)) {
return;
}
和mSelectedDate
指向同一天,因此mMinDate
将保持不变(即处于错误的状态mSelectedDate
)。
然后,控制流将运行到mSelectedDate < mMinDate
,然后运行到mAdapter.init
。在此功能中,getWeeksSinceMinDate
和mMinDate
的比较将以毫秒为单位:
mSelectedDate
并且由于private int getWeeksSinceMinDate(Calendar date) {
if (date.before(mMinDate)) {
throw new IllegalArgumentException("fromDate: " + mMinDate.getTime()
+ " does not precede toDate: " + date.getTime());
}
在mSelectedDate
之前的几毫秒内进行了初始化,因此会发生崩溃。
在较新的实现中,此代码被重写,因此API21 +缺少此问题。