2017-10-20 54 views
2

這個代碼日期選擇器不工作的API 26

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) 

請幫助我,我試圖通過各種渠道來解決問題。它不能解決這個問題。

回答

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.! 
     } 
+0

它不工作只API 26 –

+0

沒有getMinDate()和getMaxDate()在我的代碼 –

+0

dateStartPickerDialog.getDatePicker()setMinDate(calendarStart.getTimeInMillis())。 calendar日曆.add(Calendar.YEAR,543); calendarStart.add(Calendar.YEAR,543);dateStartPickerDialog.getDatePicker()。setMaxDate(calendarStart.getTimeInMillis()); 刪除這些行。 –

0

設置不同的最小和最大日期日期選擇器 試試這個

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()); 
+0

運行4.4的代碼,它會崩潰的setMinDate(calendarStart.getTimeInMillis())由於calendarStart.getTimeInMillis()在我的例子看一看上面的感謝! –

+0

這同我的代碼沒有什麼不同 –