Unable to instantiate activity… Caused by ClassNotFoundException

吃可爱长大的小学妹 提交于 2019-12-29 06:53:13

问题


After converting a perfectly working application to a library (including its Activity class!), I am trying to create an application that uses that entire library by simply superclassing the library's activity:

package com.example.baseapp.paid;

import android.os.Bundle;
import com.example.baseapp.LibActivity;


public class PaidActivity extends LibActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

Eclipse builds this newly "re-architected" application without any errors, but when I try to run it, I get an exception:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.baseapp.paid/com.example.baseapp.paid.PaidActivity}: java.lang.ClassNotFoundException: com.example.baseapp.paid.PaidActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.baseapp.paid-1.apk]
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.example.baseapp.paid.PaidActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.baseapp.paid-1.apk]
    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
    ... 11 more

I have no idea why this is happening since the error for "Class Not Found" when the class is exactly the derived class built (without errors!) and now trying to run.

How do I troubleshoot this?

What am I missing?

EDIT (answering question by @CaspNZ):

This is the AndroidManifest.xml of the library project:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.baseapp"
      android:versionCode="5"
      android:versionName="1.1.2"> 
    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET"/>   
</manifest>

And this is the AndroidManifest.xml of the application, using the library project:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.baseapp.paid"
      android:versionCode="5"
      android:versionName="1.1.2"> 
    <uses-sdk android:minSdkVersion="8" />
    <uses-library android:name="AppLibrary" />

    <uses-permission android:name="android.permission.INTERNET"/>   

    <application android:icon="@drawable/icon">
        <activity android:name=".PaidActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.baseapp.LibActivity" android:label="com.example.baseapp.LibActivity:string/rx_label">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="EditPreferences" 
                android:label="com.example.baseapp.LibActivity:string/app_name">
        </activity>
    </application>
</manifest>

I would appreciate any hint trying to troubleshoot this as this is my first time ever trying to use a library project and I am not familiar with the "tricks" involved to get this working.


回答1:


For anyone who updated to ADT 22, make sure that you have checked Android Private Libraries in the Java Build Path > Order and Export tab. For further information, read this. This works for me. Maybe little force to move users to Android Studio instead of Eclipse:))




回答2:


This can happen if you list the activities in the library manifest instead of in the application manifest. There's no need to have <activity> tags in a library manifest. (See, for instance, this thread.)




回答3:


I finally solved the problem. It turns out that I had 2 critical settings in the Properties of both projects not set correctly:

  1. In the library project, "Is Library" was not checked for some reason. I could swear that I checked it, but knowing how whimsical the Android development environment under Eclipse can be, I suspect that it was unchecked by Eclipse (or the ADT plugin) as a result of some glitch.
  2. In the application project, I neglected to add my library project as a reference via the Add... button. (how dumb could I be?)

I hope that other newbies like me will find this information helpful. Sometimes, an extremely difficult problem to debug, hides a configuration error when the innocent unsuspecting stressed programmer assumes that this is already has been taken care of...




回答4:


I had the same error in one of my projects and thought that I can share how I fixed mine.

At some point of my project I had to change one of the package names I am using where all my Activity classes are situated. When I did, I forgot to update the AndroidManifest which caused me the same error. Updating this fixed it.



来源:https://stackoverflow.com/questions/6325826/unable-to-instantiate-activity-caused-by-classnotfoundexception

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