我有一个由开始日期和结束日期组成的时间间隔。我有办法让用户通过 DatePickerDialogs 设置这些日期。以下是创建和显示对话框的方法的代码:
private void editInterval(boolean beginning) {
DatePickerDialog dpd;
Calendar cal = Calendar.getInstance();
if (beginning) {
dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
someTimeDuringYear1 = year;
someTimeDuringMonth1 = monthOfYear + 1;
someTimeDuringDay1 = dayOfMonth;
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.YEAR, year);
cal1.set(Calendar.MONTH, monthOfYear);
cal1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.YEAR, someTimeDuringYear2);
cal2.set(Calendar.MONTH, someTimeDuringMonth2);
cal2.set(Calendar.DAY_OF_MONTH, someTimeDuringDay2);
if (cal1.getTimeInMillis() > cal2.getTimeInMillis()) {
someTimeDuringYear2 = someTimeDuringYear1;
someTimeDuringMonth2 = someTimeDuringMonth1;
someTimeDuringDay2 = someTimeDuringDay1;
}
updateSomeTimeDuring();
editDepartureInterval();
}
}, someTimeDuringYear1, someTimeDuringMonth1 - 1, someTimeDuringDay1);
dpd.setTitle(getString(R.string.beginning_of_interval));
cal.add(Calendar.MONTH, 6);
dpd.getDatePicker().setMaxDate(cal.getTimeInMillis());
cal.add(Calendar.MONTH, -6);
cal.add(Calendar.DATE, 2);
dpd.getDatePicker().setMinDate(cal.getTimeInMillis());
} else {
dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
someTimeDuringYear2 = year;
someTimeDuringMonth2 = monthOfYear + 1;
someTimeDuringDay2 = dayOfMonth;
updateSomeTimeDuring();
editDepartureInterval();
}
}, someTimeDuringYear2, someTimeDuringMonth2 - 1, someTimeDuringDay2);
dpd.setTitle(getString(R.string.end_of_interval));
cal.add(Calendar.MONTH, 6);
dpd.getDatePicker().setMaxDate(cal.getTimeInMillis());
cal.set(someTimeDuringYear1, someTimeDuringMonth1 - 1, someTimeDuringDay1);
dpd.getDatePicker().setMinDate(cal.getTimeInMillis());
}
dpd.show();
dpd.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
该beginning
变量的值决定了用户是否选择了编辑时间间隔的开始日期或结束日期。这些someTimeDuringYear1
和类似的是与开始日期相关的成员变量,someTimeDuringYear2
并且类似的是与间隔的结束日期相关的。所有这些都是在创建和显示此对话框之前预设的。
编辑开始和结束间隔时抛出异常。假设我选择编辑间隔的结束日期。一切正常,直到我将日期设置为我被允许的最后日期。如果我这样做并确认,一切似乎仍然很好,但是当我再次尝试编辑间隔的结束日期时,应用程序崩溃并引发异常。(如果我将间隔的开始日期设置为最后允许的日期,然后再次尝试编辑它,也会发生同样的事情。)
这是来自 LogCat 的日志:
03-28 17:18:16.901: E/AndroidRuntime(6112): FATAL EXCEPTION: main
03-28 17:18:16.901: E/AndroidRuntime(6112): java.lang.IllegalArgumentException: Time not between Sat Mar 30 17:18:16 CET 2013 and Sat Sep 28 17:18:16 CEST 2013
03-28 17:18:16.901: E/AndroidRuntime(6112): at android.widget.CalendarView.goTo(CalendarView.java:805)
03-28 17:18:16.901: E/AndroidRuntime(6112): at android.widget.CalendarView.setMinDate(CalendarView.java:487)
03-28 17:18:16.901: E/AndroidRuntime(6112): at android.widget.DatePicker.setMinDate(DatePicker.java:315)
03-28 17:18:16.901: E/AndroidRuntime(6112): at com.skypicker.android.MainActivity.editInterval(MainActivity.java:344)
03-28 17:18:16.901: E/AndroidRuntime(6112): at com.skypicker.android.MainActivity.access$10(MainActivity.java:296)
03-28 17:18:16.901: E/AndroidRuntime(6112): at com.skypicker.android.MainActivity$5.onClick(MainActivity.java:261)
03-28 17:18:16.901: E/AndroidRuntime(6112): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:924)
03-28 17:18:16.901: E/AndroidRuntime(6112): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
03-28 17:18:16.901: E/AndroidRuntime(6112): at android.widget.AbsListView.performItemClick(AbsListView.java:1063)
03-28 17:18:16.901: E/AndroidRuntime(6112): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2519)
03-28 17:18:16.901: E/AndroidRuntime(6112): at android.widget.AbsListView$1.run(AbsListView.java:3173)
03-28 17:18:16.901: E/AndroidRuntime(6112): at android.os.Handler.handleCallback(Handler.java:605)
03-28 17:18:16.901: E/AndroidRuntime(6112): at android.os.Handler.dispatchMessage(Handler.java:92)
03-28 17:18:16.901: E/AndroidRuntime(6112): at android.os.Looper.loop(Looper.java:137)
03-28 17:18:16.901: E/AndroidRuntime(6112): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-28 17:18:16.901: E/AndroidRuntime(6112): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 17:18:16.901: E/AndroidRuntime(6112): at java.lang.reflect.Method.invoke(Method.java:511)
03-28 17:18:16.901: E/AndroidRuntime(6112): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
03-28 17:18:16.901: E/AndroidRuntime(6112): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
03-28 17:18:16.901: E/AndroidRuntime(6112): at dalvik.system.NativeStart.main(Native Method)
我整个下午都在寻找答案并试图提出解决方案。任何帮助表示赞赏。
谢谢你。
PS:这是我在这里问的第一个问题,所以如果有什么问题我很抱歉。我很乐意提供有关正在发生的事情的任何缺失信息。
编辑:奇怪的是,即使当我尝试设置时问题就出现了minDate
,但实际上似乎日期比maxDate
. 我相信这一点的原因是,如果我传递someTimeDuringDay2 - 1
而不是传递someTimeDuringDay2
给DatePickerDialog
' 的构造函数,问题就消失了。但是然后“天”字段DatePicker
确实比我想要的小一。我需要将其设置为显示someTimeDuringDay2
时DatePickerDialog
,而不是someTimeDuringDay2 - 1
. 但是当日期与它相同时,它似乎有问题maxDate
。