ClassNotFoundException Android

百般思念 提交于 2019-11-27 15:14:12
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.

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.

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.

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.

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?

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.

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.

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