21

I am making a simple alarm app. Wherever I am using Calendar class it's showing an error same as title.

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);

Whenever I build my project, it builds fine but app crashes when it's launched on phone. Below is the Logcat:

07-22 18:24:15.607 18047-18047/com.example.panwa.afinal D/AndroidRuntime: Shutting down VM
07-22 18:24:15.607 18047-18047/com.example.panwa.afinal W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40d952d0)
07-22 18:24:15.627 18047-18047/com.example.panwa.afinal E/AndroidRuntime: ***FATAL EXCEPTION: main
  java.lang.NoClassDefFoundError: android.icu.util.Calendar***
      at com.example.panwa.afinal.MainActivity.startAt10(MainActivity.java:70)
      at com.example.panwa.afinal.MainActivity$3.onClick(MainActivity.java:46)
      at android.view.View.performClick(View.java:4275)
      at android.view.View$PerformClick.run(View.java:17434)
      at android.os.Handler.handleCallback(Handler.java:615)
      at android.os.Handler.dispatchMessage(Handler.java:92)
      at android.os.Looper.loop(Looper.java:177)
      at android.app.ActivityThread.main(ActivityThread.java:4947)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:511)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
      at dalvik.system.NativeStart.main(Native Method)
07-22 18:25:33.824 18047-18047/com.example.panwa.afinal I/Process: Sending signal. PID: 18047 SIG: 9

I have a set my min SDK to be 11 but still it's not working.

earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
Shivam Panwar
  • 396
  • 1
  • 3
  • 9
  • The other solution you may consider if your app uses dates and times, is getting [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). Its date and time classes are generally much more programmer-friendly than the now very old `Calendar` class — no matter if we are talking `android.icu.util.Calendar` or `java.util.Calendar`. – Ole V.V. May 23 '17 at 08:23

1 Answers1

62

Instead of importing android.icu.util.calendar, try importing java.util.Calendar.

The android.icu.util.calendar is the ICU replacement for java.util.Calendar but it's only available starting from API 24. So, it will only work on the devices which is API 24 and above.

Ref :

Ye Lin Aung
  • 11,234
  • 8
  • 45
  • 51
  • Why did android make a seperate pacakge which does nearly the same set of functions and also made it not possible to work below API 24? And is there any advantages of using android.icu.util over java.util? – kks21199 Aug 17 '18 at 19:31
  • @kks21199 your concern is so valid. These fellows try to make life of a developer very difficult. – Makari Kevin Nov 17 '19 at 04:10