Android to Unable to instantiate Application java.lang.ClassNotFoundException:
android
Solution
I think the problem is with getApplication() which I have used in 10 different place. So I have used singleton pattern to solve this.
public class MyApplication extends Application {
private static MyApplication me;
@Override
public void onCreate() {
super.onCreate();
me = this ;
}
public static MyApplication getInstance() {
return me;
}
}
Now I have used getApplication() like this
MyApplication application = MyApplication.getInstance();
insted of
MyApplication application = (MyApplication) getApplication();
I have uploaded the fixed version on the market & now waiting if there is anymore this kind of crash. If everything goes perfect ( if no more crash in 2 weeks) then I will close the question. In meanwhile anyone has better idea or know the solution , please share it. Thanks,
Problem
I am working on application which is host on android market. Sometimes (once a month ) I got a crash report: Unable to instantiate application java.lang.ClassNotFoundException App downloads are between 10,000-50,000. I don't know why this exceptions raise on some devices not all ( I tested it on 3 different devices & I couldn't re-produce it at my end). I read articles/suggestions on different android forums regarding the issue but I didn't succeed in solving it. Does anyone face similar issue & suggest me what should I do? Note: I am extending application class like this ``` public class MyApplication extends Application { } ``` I register it in the manifest.xml like this ``` <application android:icon="@drawable/app_icon" android:label="@string/my_app_name" android:name="MyApplication"> ``` Stack Trace : ``` java.lang.RuntimeException: Unable to instantiate application com.xyz.MyApplication java.lang.ClassNotFoundException: com.xyz.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.xyz-1/pkg.apk] at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:650) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4238) at android.app.ActivityThread.access$3000(ActivityThread.java:126) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2076) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4633) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassNotFoundException: com.xyz.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.xyz-1/pkg.apk] at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243) at java.lang.ClassLoader.loadClass(ClassLoader.java:573) at java.lang.ClassLoader.loadClass(ClassLoader.java:532) at android.app.Instrumentation.newApplication(Instrumentation.java:942) at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:645) ``` I don't know why application crash on some devices not all.