java ClassNotFoundException Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3090009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:18:04  来源:igfitidea点击:

ClassNotFoundException Android

javaandroiddalvikclassnotfoundexceptionclassnotfound

提问by jakehschwartz

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:

所以我今天在处理我的 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)

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 ClassNotFoundExceptionis thrown?

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

Thanks, Jake

谢谢,Hyman

回答by jakehschwartz

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.

这是我目前拥有的代码,我相信它可以解决这个问题。MyClass 只是我项目中的一个类。就像我说的,一位同事向我展示了它,但它似乎很简单。

回答by Stephen C

Are there any other reasons a ClassNotFoundException is thrown?

是否有任何其他原因抛出 ClassNotFoundException?

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.

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

回答by Janusz

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 listto see which classes are supported and which are not supported.

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

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

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

回答by krishnakumarp

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.

对于 android 应用程序,我会遇到与外部 jar 文件相同的问题。在我的情况下,解决方案是将 jar 从“lib”文件夹移动到 android 默认的“libs”文件夹。

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.

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

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

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

Adding a library/JAR to an Eclipse Android project

将库/JAR 添加到 Eclipse Android 项目

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

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

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

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

回答by steveh

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

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

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.

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

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.

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

回答by BenL

Are there any other reasons a ClassNotFoundException is thrown?

是否有任何其他原因抛出 ClassNotFoundException?

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.

就我而言,在使用 eclipse 构建 apk 时,我的主要活动遇到了 ClassNotFoundException,但在使用 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.

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

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

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