当 backupAgent 尝试启动时,为什么我会收到 ClassNotFoundException

发布于 2024-12-06 06:00:48 字数 1979 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花落人离去 2024-12-13 06:00:49

对我来说,设备上似乎安装了重复的软件包。如果我是你,我会测试:

  • 包名称是否被无意中更改。
  • 您是否尝试过清理项目、从测试设备中卸载应用程序、重新构建并重新安装它?

我知道这些问题对你来说可能看起来像《显而易见的船长》,但我无法告诉你这两件事救了我多少次。

To me it seems like there's a duplicate package being installed on the device. If I were you, I'd test:

  • Has the package name been inadvertently been changed.
  • Have you tried to clean the project, uninstall the app from your test device, re-build and re-installed it?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文