Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to instantiate activity... Caused by ClassNotFoundException

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.

like image 634
an00b Avatar asked Jun 13 '11 01:06

an00b


1 Answers

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:))

like image 113
nlt Avatar answered Oct 27 '22 10:10

nlt