Android Activity ClassNotFoundException - tried everything

前端 未结 15 2137
有刺的猬
有刺的猬 2020-11-22 12:02

I\'ve just refactored an app into a framework library and an application, but now when I try and start the app in the emulator I get the following error stack trace:

相关标签:
15条回答
  • 2020-11-22 12:48

    solution provided by @yorkw is absolutely correct. I had an additional issue, in one of the layout files, I had mentioned the absolute name incorrectly. It should have been

    <android.support.v4.view.ViewPager xmlns:android="...
    

    rather I had put it

    <android.support.v4.app.view.ViewPager xmlns:android="...
    

    after resolving these, it worked good.

    0 讨论(0)
  • 2020-11-22 12:55

    For Eclipse Kepler(Mac): Control-click project -> Android Tools -> add support library

    0 讨论(0)
  • 2020-11-22 12:56

    What interfaces or parent classes does SimonSaysActivity use/extend and are they available on the version of Android you're using? I've seen similar problems where my activity implemented an API interface that wasn't available on the version of Android I was using.

    In my case my activity implemented the interface PopupMenu.OnDismissListener which is only available in API 14 but when I ran the app on my 2.3 device I received a java.lang.NoClassDefFoundError exception for the activity.

    0 讨论(0)
  • 2020-11-22 12:58

    I spent some time play with my own project, and I am able to replicate your problem and get exactly the same exception stack trace when trying to run my main project, so I think this could be the cause:

    Just like what I thought, it is all about how you reference your Android library project in the Android main project, a simple Eclipse configuration settings.

    The Wrong Way:
    Right click main project, choose Properties -> Java Build Path -> Projects -> Add..., this add the Android library Project as a dependency project in Android main project's build path, this does not work. If all required Android-related resources are defined in main project, you will not get any error at compile time, but when run the application, you get the exception described in the question.

    The Correct Way:
    Right click main project, choose Properties -> Android, in the Library section, add your Android library project here. Check out official dev guide Referencing a library project. This should fix all your problem. Also note that you have to use relative path reference the actual Android library project, as stated in the Library Project - Development considerations.

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 12:59

    i've tested the code that you've given , and it works fine. try changing the "extends SimonSaysActivity " to simply "extends Activity " and see for yourself that it works .

    the reason that it doesn't work is either SimonSaysActivity doesn't extend Activity (which i don't think you've made this mistake) , or the order of the build path is wrong .

    to go to the order of the build path , go to :

    project->properties->Java build path->order and export .
    

    my basic order is : project src , project gen , android 4.0.3 , android dependencies .

    this problem usually occurs when you use libraries .

    0 讨论(0)
  • 2020-11-22 13:00

    classNotFoundException in Java is a subclass of java.lang.Exception and Comes when Java Virtual Machine tries to load a particular class and doesn't found the requested class in classpath.

    Read more: http://javarevisited.blogspot.com/2011/08/classnotfoundexception-in-java-example.html#ixzz3yX3WBeFA

    Try going to Project -> Properties -> Java Build Path -> Order & Export And Confirm Android Private Libraries are checked for your project and for all other library projects you are using in your Application.

    How to deal with the ClassNotFoundException

    1. Verify that the name of the requested class is correct and that the appropriate .jar file exists in your classpath. If not, you must explicitly add it to your application’s classpath.
    2. In case the specified .jar file exists in your classpath then, your application’s classpath is getting overriden and you must find the exact classpath used by your application.
    3. In case the exception is caused by a third party class, you must identify the class that throws the exception and then, add the missing .jar files in your classpath.
    0 讨论(0)
提交回复
热议问题