7

This is a very weird problem. My app that runs just fine but somehow if I leave my phone for an hour or two while my app is running, I get the following error when I come back to it later:

java.lang.NoClassDefFoundError: yoga.database.Manager
at
yoga.YogaActivity.openDatabase(YogaActivity.java:294)
at
yoga.YogaActivity.initData(YogaActivity.java:275)
at
yoga.YogaActivity.onCreate(YogaActivity.java:102)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access$2100(ActivityThread.java:116)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4203)
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:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
at dalvik.system.NativeStart.main(Native Method)

I know for a fact that my yoga.database.Manager class was loaded at the time when the app was launched, but somehow the class must have been unloaded by the system as I left the phone.

Does Android unload classes automatically after certain periods? What can I do when my class gets unloaded?

The above error causes my app to crash, but I can easily re-launch it and it runs just fine.

The problem occurs mostly on my HTC Magic phone running Android 1.6.

4

2 回答 2

9

The Dalvik VM doesn't currently unload classes. If it did, it would only be able to do so when all classes associated with a particular class loader could be unloaded at once, which will not be the case while your app is running.

You need to check the logcat output for errors leading up to this exception. One way to get a NoClassDefFoundError is for something to fail during initialization of the class in question; if that happened there would likely be a trail in the log.

(Of course, by now the logcat output is probably long gone, but if the problem is repeatable you'll want to capture it next time.)

于 2010-03-19T20:03:42.157 回答
2

你在玩自定义类加载器的游戏吗?

我问,因为如果yoga.YogaActivityyoga.database.Manager是同一个 Android APK 文件中的标准 Java 类,你不应该有一个在内存中,而另一个没有。我从未见过 Android 一次性卸载类——至少,我以前从未遇到过这个特殊问题。

还:

当我的应用程序运行时,我会离开手机一两个小时

你可能想澄清你的意思。你的意思是你把你的活动留在前台吗?你的意思是你按HOME然后几个小时后回来?你的意思是别的吗?

于 2010-03-15T15:01:05.857 回答