当 backupAgent 尝试启动时,为什么我会收到 ClassNotFoundException
我使用 BackupAgentHelper
来备份 Android 应用中的 SharedPreferences
。我已经在模拟器(Android 1.6 和 2.2)和我自己的手机(Android 2.3.3)上对其进行了测试,一切运行良好。然而,今天我在开发者控制台中收到了一个崩溃报告,如下所示:
java.lang.RuntimeException: Unable to create BackupAgent com.xxx.yyy.MyBackupAgent: java.lang.ClassNotFoundException: com.xxx.yyy.MyBackupAgent in loader dalvik.system.PathClassLoader[/mnt/asec/com.xxx.yyy-2/pkg.apk]
at android.app.ActivityThread.handleCreateBackupAgent(ActivityThread.java:2114)
at android.app.ActivityThread.access$3200(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1138)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.xxx.yyy.MyBackupAgent in loader dalvik.system.PathClassLoader[/mnt/asec/com.xxx.yyy-2/pkg.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at android.app.ActivityThread.handleCreateBackupAgent(ActivityThread.java:2064)
... 10 more
backupAgent 在清单中的应用程序标记中声明为:
android:backupAgent="com.xxx.yyy.MyBackupAgent"
显然,类 MyBackupAgent
is 存在因为我可以构建 .apk
并且它在多个设备上运行得很好。那么这里怎么会找不到这个类呢?我在上面的消息中注意到的一件事是,该应用程序似乎安装在一个路径中,该路径具有我的包名称并在末尾附加“-2”。这是否会导致类加载器看不到我的包中的类,因为我在 android:backupAgent
中指定了完整的包名称和类,或者该部分是否不相关?有人能理解找不到该类的原因是什么吗?
值得一提的是,我的应用程序可以安装在 SD 卡上。
请原谅我将上面消息中的真实包名称替换为 com.xxx.yyy。
I use the BackupAgentHelper
to backup the SharedPreferences
in my Android app. I have tested it in the emulator (Android 1.6 and 2.2) and on my own phone (Android 2.3.3) and it all works well. However, today I got a crash report in the Developer Console looking like this:
java.lang.RuntimeException: Unable to create BackupAgent com.xxx.yyy.MyBackupAgent: java.lang.ClassNotFoundException: com.xxx.yyy.MyBackupAgent in loader dalvik.system.PathClassLoader[/mnt/asec/com.xxx.yyy-2/pkg.apk]
at android.app.ActivityThread.handleCreateBackupAgent(ActivityThread.java:2114)
at android.app.ActivityThread.access$3200(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1138)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.xxx.yyy.MyBackupAgent in loader dalvik.system.PathClassLoader[/mnt/asec/com.xxx.yyy-2/pkg.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at android.app.ActivityThread.handleCreateBackupAgent(ActivityThread.java:2064)
... 10 more
The backupAgent is declared in the application tag in the Manifest as:
android:backupAgent="com.xxx.yyy.MyBackupAgent"
Apparantly, the class MyBackupAgent
is present since I can build the .apk
and it runs just fine on several devices. So how can it be that it does not find the class here? One thing I notice in the message above is that the app seems to be installed in a path which have my package name AND an appended "-2" at the end. Can this cause the classloader to not see the class in my package since I specify the full package name and class in the android:backupAgent
, or is that part irrelevant? Can anybody understand what the reason can be that the class cannot be found?
Worth to mention is that my app can be installed on SD card.
Excuse me for replacing my real package name with com.xxx.yyy in the message above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,设备上似乎安装了重复的软件包。如果我是你,我会测试:
我知道这些问题对你来说可能看起来像《显而易见的船长》,但我无法告诉你这两件事救了我多少次。
To me it seems like there's a duplicate package being installed on the device. If I were you, I'd test:
I know these questions may seem like Captain Obvious to you but I can't tell you how many times these two things have saved me.