Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException Android

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

like image 614
jakehschwartz Avatar asked Jun 22 '10 03:06

jakehschwartz


People also ask

What is classnotfoundexception in Java?

This exception java.lang.classnotfoundexception extends the ReflectiveOperationException, which is defined as the common superclass of exceptions thrown by reflective operations in core reflection. Finally, after the Java 1.4 release, the ClassNotFoundException has been retrofitted to conform to the general purpose exception-chaining mechanism.

What is NoClassDefFoundError and how to fix it?

Another scenario when it can happen is the class you are trying to load is not a valid class. NoClassDefFoundError is an error and it occurs when a class is present at compile-time and the same is missing at the run time. This is a fatal error and happens when you try to instantiate class or when you try to call a static method.

Why do I get an error when trying to load a class?

This exception is thrown when an application tries to load a class through its string name, but no definition for the specified class name could be found. A class can be loaded using one of the following methods: The forName method that resides inside the Class class. The findSystemClass method that resides inside the ClassLoader class.

How to fix Android Studio package name error?

The solution is to check among your xml files (in the folder layout priority) if there is no error writing the package name. This kind of error is not automatically detected by android studio until the Programs crash in your phone or emulator. Worse, the search for the package via android studio did not allow us to find the cause.


1 Answers

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.

like image 74
jakehschwartz Avatar answered Oct 31 '22 18:10

jakehschwartz