ClassNotFoundException Android

给你一囗甜甜゛ 提交于 2019-12-17 13:01:12

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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?




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/3090009/classnotfoundexception-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!