日期选择器不适用于API 26

时间:2017-10-20 04:25:29

标签: java android

此代码

Calendar calendarStart = Calendar.getInstance();
    startYear = calendarStart.get(Calendar.YEAR) + 543;
    startMonth = calendarStart.get(Calendar.MONTH);
    startDay = calendarStart.get(Calendar.DAY_OF_MONTH) - 7;

    dateStartPickerDialog = new DatePickerDialog(
            AccountReportActivity.this, R.style.DatePicker,
            AccountReportActivity.this, startYear, startMonth, startDay);

    dateStartPickerDialog.getDatePicker().setMinDate(calendarStart.getTimeInMillis());
    calendarStart.add(Calendar.YEAR, 543);
    dateStartPickerDialog.getDatePicker().setMaxDate(calendarStart.getTimeInMillis());

此错误代码

 at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                     Caused by: java.lang.IllegalArgumentException: timeInMillis must be between the values of getMinDate() and getMaxDate()
                                                                        at android.widget.DayPickerView.setDate(DayPickerView.java:307)
                                                                        at android.widget.DayPickerView.setDate(DayPickerView.java:286)
                                                                        at android.widget.DayPickerView.setDate(DayPickerView.java:275)
                                                                        at android.widget.DatePickerCalendarDelegate.onDateChanged(DatePickerCalendarDelegate.java:405)
                                                                        at android.widget.DatePickerCalendarDelegate.init(DatePickerCalendarDelegate.java:376)
                                                                        at android.widget.DatePicker.init(DatePicker.java:228)
                                                                        at android.app.DatePickerDialog.<init>(DatePickerDialog.java:133)
                                                                        at android.app.DatePickerDialog.<init>(DatePickerDialog.java:109)
                                                                        at com.splanet.aplaysellers.activity.report.AccountReportActivity.setUpCurrentStartDate(AccountReportActivity.java:103)
                                                                        at com.splanet.aplaysellers.activity.report.AccountReportActivity.initWidget(AccountReportActivity.java:75)
                                                                        at com.splanet.aplaysellers.activity.report.AccountReportActivity.onCreate(AccountReportActivity.java:51)
                                                                        at android.app.Activity.performCreate(Activity.java:6980)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)

请帮助我,我尝试通过各种渠道解决问题。它无法解决这个问题。

2 个答案:

答案 0 :(得分:3)

使用System.currentTimeMillis()作为最大日期,这将以毫秒为单位返回当前日期。并且最少使用日历。! 如果您希望您的应用在每个Android版本上完全正常工作,请按照我的示例代码进行操作。 Date Picker Example

 Calendar calendar = Calendar.getInstance();

        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);

        datePickerDialog = new DatePickerDialog(getActivity(), onDateSet,
                year, month, day);

        // calendar.add(Calendar.DATE, Constants.DateAndMonth.CURRENT_DAY);//Current day
        datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
        // Set the Calendar new date as maximum date of date picker
        datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());

        // Subtract 90 days from Calendar updated date
        calendar.add(Calendar.DATE, -Constants.DateAndMonth.LAST_NINETY_DAY);

        // Set the Calendar new date as minimum date of date picker
        datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            datePickerDialog.setTitle("");//Prevent Date picker from creating extra Title.!
        }

答案 1 :(得分:-1)

将不同的最小和最大日期设置为datepicker 试试这个

Calendar calendarStart = Calendar.getInstance();
startYear = calendarStart.get(Calendar.YEAR);
startMonth = calendarStart.get(Calendar.MONTH);
startDay = calendarStart.get(Calendar.DAY_OF_MONTH) - 7;

dateStartPickerDialog = new DatePickerDialog(
        AccountReportActivity.this, R.style.DatePicker,
        AccountReportActivity.this, startYear, startMonth, startDay);

dateStartPickerDialog.getDatePicker().setMinDate(calendarStart.getTimeInMillis());
calendarStart.add(Calendar.YEAR, 543);
dateStartPickerDialog.getDatePicker().setMaxDate(calendarStart.getTimeInMillis());