androidclassnotfoundexceptionandroid-4.2-jelly-beanandroid-4.3-jelly-beanandroid-4.1-jelly-bean

Caused by: java.lang.ClassNotFoundException: .ui.activities.MainActivity on devices lower API Level 19


I'm getting this strange error on the pre KitKat devices:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{me.bitfrom.whattowatch/me.bitfrom.whattowatch.ui.activities.MainActivity}: java.lang.ClassNotFoundException: me.bitfrom.whattowatch.ui.activities.MainActivity
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                                                                          at android.app.ActivityThread.access$600(ActivityThread.java:130)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                          at android.os.Looper.loop(Looper.java:137)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                          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:786)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                          at dalvik.system.NativeStart.main(Native Method)
                                                                       Caused by: java.lang.ClassNotFoundException: me.bitfrom.whattowatch.ui.activities.MainActivity
                                                                          at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
                                                                          at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
                                                                          at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
                                                                          at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                                                                          at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                          at android.os.Looper.loop(Looper.java:137) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:4745) 
                                                                          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:786) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                                                                          at dalvik.system.NativeStart.main(Native Method) 

On devices with API Level >= 19 everything OK. I thought that it could be dex limit, but this approach didn't help.

UPD: My MainActivity implements Transition.TransitionListener, but I'm using @SuppressLint("NewApi") annotation.


Solution

  • My MainActivity implements Transition.TransitionListener

    That's probably your problem. That interface only exists on API Level 19 and higher. Older devices do not have that interface, and therefore cannot load your class.

    but I'm using @SuppressLint("NewApi") annotation

    All that does is prevent your IDE from complaining. It does not cause older devices to get Transition or Transition.TransitionListener.

    Rather than implementing it on the activity, implement that interface on something else that you only use on API Level 19+ devices, such as an anonymous inner class instance.