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

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.

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值