gpt4 book ai didi

java - 日期选择器不适用于 API 26

转载 作者:搜寻专家 更新时间:2023-11-01 09:31:36 26 4
gpt4 key购买 nike

这段代码

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)

请帮助我,我尝试通过各种 channel 解决问题。它不能解决这个问题。

最佳答案

对最大日期使用 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.!
}

关于java - 日期选择器不适用于 API 26,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46842365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com