Home » Android » java – API21 new DatePicker ShowWeekNumber Crash

java – API21 new DatePicker ShowWeekNumber Crash

Posted by: admin May 14, 2020 Leave a comment

Questions:

I was testing my app with the new Android 5.0 (API21) but was getting an error with the CalendarView

Caused by: java.lang.UnsupportedOperationException: CalendarView does not exists for the new DatePicker
   at android.widget.DatePickerCalendarDelegate.getCalendarView(DatePickerCalendarDelegate.java:501)
   at android.widget.DatePicker.getCalendarView(DatePicker.java:365)

For my surprise the error was not because there is no CalendarView in the NEW API21 DatePicker, in fact, there is no DatePicker in API21, just CalendarView.

The error was produced because I was hidding the Week Numbers in the CalendarView

picker.getCalendarView().setShowWeekNumber(false);

This instruction throws the UnsupportedOperationException, be warned no more Week Numbers in the CalendarView, they just disappeared it, no explanation.

How to&Answers:

One problem that can cause this exception is that if your DatePicker mode is android:datePickerMode="calendar", DatePicker.getCalendarView would cause exception in post API 21, then in order to solve this, change the mode of your DatePicker to android:datePickerMode="spinner" and in java you can use from conditions to handle API greater than or equal to 21

Answer:

I think you are getting confused.
your requirement is “to hide week number”
But actually there is no week Number in date picker neither in api21 nor below 21

so i suggest you skipping “picker.getCalendarView().setShowWeekNumber(false);”
line and continue with rest of code.