Android 类NotFoundException

发布于 2024-09-06 08:56:46 字数 527 浏览 8 评论 0原文

今天我在开发 Android 程序时遇到了一个问题。我有一个类,可以将 XML 字符串转换为 Java 对象(第三方),它在常规 java 项目中工作正常,但在 Android 上我收到这个奇怪的错误:

06-21 22:44:26.402: DEBUG/App(259): java.lang.ClassNotFoundException: com.package.mycode.Class in loader dalvik.system.PathClassLoader@4001b500
06-21 22:44:26.402: DEBUG/App(259):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)

由于显而易见的原因,我隐藏了我的应用程序名称和包,但我想知道是否有人遇到过这样的问题。类位于正确的包中,这是我添加的库。我之前参考过的其他课程都在那里并且可以制作。还有其他原因引发 ClassNotFoundException 吗?

谢谢, 杰克

So I ran into a problem today while working on my Android program. I have a class that turns that an XML string into a Java object (third party) and it works fine in as a regular java project but on Android I get this weird error:

06-21 22:44:26.402: DEBUG/App(259): java.lang.ClassNotFoundException: com.package.mycode.Class in loader dalvik.system.PathClassLoader@4001b500
06-21 22:44:26.402: DEBUG/App(259):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)

I hide my application name and my package for obvious reasons but I was wondering if anyone has ever encountered problems like this. Class is in the correct package, which is a library I have added. Other classes that I reference before are there and those can be made. Are there any other reasons a ClassNotFoundException is thrown?

Thanks,
Jake

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

回眸一笑 2024-09-13 08:56:46
private static void fixClassLoaderIssue()
{
    ClassLoader myClassLoader = MyClass.class.getClassLoader();
    Thread.currentThread().setContextClassLoader(myClassLoader);
}

这是我目前拥有的代码,我相信它解决了这个问题。 MyClass 只是我项目中的一个类。就像我说的,一位同事向我展示了它,但它看起来非常简单。

private static void fixClassLoaderIssue()
{
    ClassLoader myClassLoader = MyClass.class.getClassLoader();
    Thread.currentThread().setContextClassLoader(myClassLoader);
}

This is the code I currently have that I believe fixed this problem. MyClass is just a class I have in my project. Like I said, a co-worked showed it to me, but it seems pretty straight forward.

无远思近则忧 2024-09-13 08:56:46

还有其他原因引发 ClassNotFoundException 吗?

IIRC,如果“com.package.mycode.Class”的静态依赖项中的其他类无法加载,或者静态初始化过程中出现问题,也会抛出该异常。但这些应该显示为嵌套异常......第一次尝试加载类

Are there any other reasons a ClassNotFoundException is thrown?

IIRC, it can also be thrown if some other class in the static dependencies of "com.package.mycode.Class" cannot be loaded, or if there is a problem during static initialization. But these should show up as a nested exception ... the first time you attempt to load the class.

如此安好 2024-09-13 08:56:46

Android 并不具有普通机器上可用的所有 Java 类。
缺少一些类可能会导致此错误。但这应该已经在编译时显示。查看此列表以了解支持哪些类以及哪些不受支持。

同样,我只是猜测,因为编译器应该看到丢失的类。

Android has not all Java classes available that are available on a normal machine.
There are some classes missing that could cause this error. But this should be already be shown at compile time. Look at this list to see which classes are supported and which are not supported.

Again I'm only guessing because the compiler should see the missing classes.

傲世九天 2024-09-13 08:56:46

对于 Android 应用程序,我对外部 jar 文件也有同样的问题。就我而言,解决方案是将 jar 从“lib”文件夹移动到 android 默认的“libs”文件夹。

当 jar 位于 lib 文件夹中(也添加到构建路径中)时,虽然 Eclipse 中没有构建问题,但应用程序在运行时给出 ClassNotFoundException。一旦我将 jar 移至“libs”文件夹,该 jar 就开始出现在“Android 依赖项”下,并且应用程序开始正常工作。

这些是关于同一主题的一些其他讨论。

将库/JAR 添加到 Eclipse Android 项目

Android:文件夹是什么jar 文件的名称(LIB 或 LIBS)?

使用 Android 生成的 ant 构建文件时如何为 JAR 指定 lib 文件夹?

For an android app, I'd same issue with an external jar file. In my case the solution was to move the jar from "lib" folder to the android default "libs" folder.

When the jar was in lib folder (was added to build path as well), while there are was no build issue in Eclipse, the app was giving ClassNotFoundException at runtime. Once I moved the jar to "libs" folder, the jar started appearing under "Android Dependencies" and app started working fine.

These are some of the other discussions about the same topic.

Adding a library/JAR to an Eclipse Android project

Android: What is the folder name of the jar files (LIB or LIBS)?

How to specify lib folder for JARs when using Android-generated ant build file?

勿忘初心 2024-09-13 08:56:46

我在启动 aynctask 时遇到问题。它曾经工作得很好,但后来我将

line = StringEscapeUtils.unescapeHtml4(line);

(它是一个 commons.apache.org 库)添加到 asynctask 中,然后出现了很多问题。它编译正常,但是当我尝试运行时,它说在我的 asynctask 类上找不到类。但当我点击恢复时,它继续执行异步任务,然后它就死了。

所以我猜问题是 StringEscapeUtils 没有正确安装,但我无法从收到的错误消息中弄清楚这一点。

i had a problem starting an aynctask. it used to work just fine but then i added

line = StringEscapeUtils.unescapeHtml4(line);

(its a commons.apache.org library) to the asynctask then lots of problems. it compiles ok but when i tried to run, it said class not found on my asynctask class. but it i hit resume it kept going in the async task, then it died.

so i guess the problem is StringEscapeUtils isn't installed properly, but i had no way of figuring that out from the error messages i was getting.

夜巴黎 2024-09-13 08:56:46

还有其他原因引发 ClassNotFoundException 吗?

就我而言,在使用 eclipse 构建 apk 时,我的主要活动遇到了 ClassNotFoundException,但在使用 Maven 构建时没有任何问题。

问题是我的主要活动是实现一个普通 java 项目“POJO 库”的接口。在 Android 项目 > 属性 > Java 构建路径 > 顺序和导出中,我必须勾选该框才能导出“POJO 库”。清理项目后,将接口包含到apk中的classes.dex中,问题解决。

当我试图理解这个问题时,我被引导到这里,我希望它能帮助处于同样情况的其他人。

Are there any other reasons a ClassNotFoundException is thrown?

In my case, I got a ClassNotFoundException for my main activity when building the apk with eclipse but did not have any problem when building with maven.

The problem was that my main activity was implementing an interface from a plain java project "POJO library". In the Android project>Properties>Java Build Path>Order and Export I had to tick the box to export the "POJO Library". After cleaning the project, the interface was included in the classes.dex in the apk and the problem was solved.

I was directed here when trying to understand the problem, and I hope it will help somebody else in the same situation.

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