安卓:什么时候类会被从系统中卸载?什么时候、系统、类会被

由网友(有个鬼故事叫开学.)分享简介:这是一个很奇怪的问题。我的应用程序运行得很好,但不知何故,如果我离开我的手机一两个小时,而我的应用程序正在运行,我得到以下错误,当我回来稍后:This is a very weird problem. My app that runs just fine but somehow if I leave my phone...

这是一个很奇怪的问题。我的应用程序运行得很好,但不知何故,如果我离开我的手机一两个小时,而我的应用程序正在运行,我得到以下错误,当我回来稍后:

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)

我知道一个事实,我yoga.database.Manager类是在当应用程序被启动,但不知何故,这个类必须被卸载的系统为我留下的电话时加载。

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.

难道Android的某些时期后自动卸载类?我该怎么办时,我的课被卸载?

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.

多发生在我运行的是Android 1.6的HTC Magic手机中的问题。

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

推荐答案

Dalvik虚拟机目前不卸载类。如果有,那就只能这样做,当与一个特定的类加载器相关联的所有类可以同时被卸载,而你的应用程序正在运行,这将不会是这样的。

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.

您需要检查的logcat输出的错误导致这一例外。得到的NoClassDefFoundError的一种方法是为某物的类有关的初始化过程中失败;如果发生有可能会是在日志中的痕迹。

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.

(当然,这时的logcat输出可能早已不复存在,但如果问题是重复的,你会想在下一次捕捉到它。)

(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.)

阅读全文

相关推荐

最新文章