11

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.

pvalle
  • 1,335
  • 1
  • 13
  • 20
  • I've had users report crashes with this same error, so now I'm going to take your advice and remove the line that hides the week numbers. Thank you for posting this! – LargeGlasses Feb 18 '15 at 22:22

2 Answers2

2

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

Elham Kohestani
  • 3,013
  • 3
  • 20
  • 29
1

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.

ajinkya gaurkar
  • 197
  • 1
  • 14