Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API21 new DatePicker ShowWeekNumber Crash

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.

like image 551
pvalle Avatar asked Nov 07 '14 06:11

pvalle


2 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

like image 156
Elham Kohestani Avatar answered Sep 27 '22 23:09

Elham Kohestani


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.

like image 31
ajinkya gaurkar Avatar answered Sep 27 '22 23:09

ajinkya gaurkar