简体   繁体   中英

“Unable to instantiate activity” error

One of my Android apps has about 100,000 users and, about 10 times a week, I get the following exception being reported to me via Google's market tool:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.rebm.asp/org.rebm.asp.MainActivity}:
  java.lang.ClassNotFoundException: org.rebm.asp.MainActivity in loader dalvik.system.PathClassLoader[/mnt/asec/org.rebm.asp-1/pkg.apk]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
at android.app.ActivityThread.access$2500(ActivityThread.java:129)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4701)
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:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: org.rebm.asp.MainActivity in loader dalvik.system.PathClassLoader[/mnt/asec/org.rebm.asp-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.newActivity(Instrumentation.java:1021)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2651)
... 11 more

From searching online, this error usually indicates something is wrong with the manifest file and the package names being used. However, I can't reproduce this error on my own devices or on the emulator. I only know of this error through the market tool. All I can conclude is that it's a rare error because I know lots of people are using the app and none of the reviews indicate it crashes.

Does anyone know what the issue might be or how I can diagnose it?

The stack trace that you give in the question contains the proof that the problem is due to moving the app to the SD card:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.rebm.asp/org.rebm.asp.MainActivity}: java.lang.ClassNotFoundException: org.rebm.asp.MainActivity in loader dalvik.system.PathClassLoader[/mnt/asec/org.rebm.asp-1/pkg.apk]

As you can see, the path to your application is contains /mnt/asec which is the Android secure application mount point.

In order to verify if this problem happens due to Apps2SD, you should check to see if all the stack traces you have include the /mnt/asec directory in the path to your application. If they are all so, you can be sure that it is an Apps2SD error.

As others have said, these kinds of problems sometime arise due to the SD card not being available when the application is being loaded or from a corrupt asec partition on the SD card.

Does your app, in any way, register itself to launch at boot or is it some sort of a widget that the system would try to load even when the SD card has not been mounted yet? If so, maybe you should turn off the option of moving the app to the SD card.

Having the same "rare" issue on all published games.

Might be an issue with installs to SD card, or moving app to the SD card, then removing the SD card.

Other guess is that the app gets downloaded or installed multiple times somehow.

I haven't been able to reproduce this so not able to nail it down either.

Well there are couple of suggestions that might help you debug your application. Hope they solve your issue. Also post your Android Manifest XML file and your MainActivity's onCreate Method snippet so that we could also help you debug it too and narrow down the possibilities ...

Suggestions:

1) In your Android XML , check if your minimum Sdk version ( android:minSdkVersion="3" ) is 3 ? If it is change it to 7+ . I have found this trick helpful sometimes while coping up with rare issues.

2) In your Android XML, check your that you have registered your Activity there. If not please register it, each new activity has to be registered before using it. If your activity is registered, recheck the path that you are supplying to it is also correct.

3) Clean the project and build it again , so that the R.java is updated and doesn't uses any cached files.

4) Migrate your project , make one new project and copy all your files there. It also solves rare issues.

5) According to one user :

I just got the same error (Unable to instantiate activity...) with Opera Mini. Opera Mini was on SD card (moved to SD card in the app setting). The error seems to be related to the fact that I swapped the SD card yesterday. The device was shutdown, I copied all data from the old card over to the new card (with cp -a) and then inserted the new card and started the device again. Everything seems to work as expected, but I see now that all apps on the SD card crashes with the same error.

Device: HTC Desire HD (Android 2.2)
Old SDHC card: SanDisk 8GB class 4
New SDHC card: Kingston 16GB class 4

So I would say that this is a an Android bug and not something that can be fixed by app developers.

Also see: http://android-developers.blogspot.com/2010/07/apps-on-sd-card-details.html

It has always been the case that when you swap SD cards on an Android device, if you physically copy the contents of the old card to the new one, the system will use the data on the new card as if nothing had changed. This is also true of apps which have been installed on the SD card."

This seems to be incorrect.

Hope one of these solve your problem.

I had this issue but not with activity. Fixed with a workaround:

ClassLoader myClassLoader = TroubleClass.class.getClassLoader();
Thread.currentThread().setContextClassLoader(myClassLoader);

It may happen that the app works fine on your eclipse and mobile , when you install it from eclipse but may crash when in the market (Happened to me), if the same happens to you, there are 2 things you need to do and you will get out of this situation.

  1. Clean the project and rebuild. (Using Project-Clean)
  2. Delete and re-include the libraries , if any.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM