ClassNotFoundException Android

jakehschwartz picture jakehschwartz · Jun 22, 2010 · Viewed 17.1k times · Source

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

Answer

jakehschwartz picture jakehschwartz · Feb 4, 2011
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.