Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - Unable to instantiate activity - ClassNotFound - Fails on one eclipse but not other

I am receiving a 'Unable to instantiate activity' error when I try to load up my program. One thing that baffles me is the exact same code works on my older PC. I just built a new one with new eclipse / android SDK / etc. I have both machines running the same code, one works fine and the other gives this error.

Any thoughts?

Edit: One thing I notice in the stack is

Caused by: java.lang.ClassNotFoundException: com.voldaran.puzzle.graBLOX.PopActivity in loader dalvik.system.PathClassLoader[/data/app/com.voldaran.puzzle.graBLOX-2.apk]

It lists graBLOX-2.apk . Why is it using '-2.apk'?

When I manually reproduce this error on the working machine, it does not display that last []'d part.

Android Manifest

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".PopActivity"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
              android:finishOnTaskLaunch="true"
              android:configChanges="orientation|keyboardHidden"
              android:screenOrientation="portrait"
              >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Stack

 04-21 17:33:42.284: E/AndroidRuntime(4045): FATAL EXCEPTION: main
04-21 17:33:42.284: E/AndroidRuntime(4045): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.voldaran.puzzle.graBLOX/com.voldaran.puzzle.graBLOX.PopActivity}: java.lang.ClassNotFoundException: com.voldaran.puzzle.graBLOX.PopActivity in loader dalvik.system.PathClassLoader[/data/app/com.voldaran.puzzle.graBLOX-2.apk]
04-21 17:33:42.284: E/AndroidRuntime(4045):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at android.os.Looper.loop(Looper.java:130)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at java.lang.reflect.Method.invokeNative(Native Method)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at java.lang.reflect.Method.invoke(Method.java:507)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at dalvik.system.NativeStart.main(Native Method)
04-21 17:33:42.284: E/AndroidRuntime(4045): Caused by: java.lang.ClassNotFoundException: com.voldaran.puzzle.graBLOX.PopActivity in loader dalvik.system.PathClassLoader[/data/app/com.voldaran.puzzle.graBLOX-2.apk]
04-21 17:33:42.284: E/AndroidRuntime(4045):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-21 17:33:42.284: E/AndroidRuntime(4045):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
like image 488
naradlov Avatar asked Dec 27 '22 02:12

naradlov


2 Answers

I was happening pretty much the same situation (project made initially in 32 bit machine, then imported in another 64 bit machine).

In Android projects under Eclipse Juno (64 bit):

  • Click mouse right button on the project in "Project Explorer", then select "Properties"
  • Select "Java Build Path" in the list at left.
  • Make click on "Order and Export" tab.
  • Mark the two checkboxes for "Android Private Libraries" and "Android Dependencies", respectively, then click OK button.
  • Clean project(s) and run.
like image 162
user2503713 Avatar answered Jan 13 '23 10:01

user2503713


Try this. Add this piece of code inside your PopActivity and call this function from inside its default constructor.

private static void fixClassLoaderIssue()
{
ClassLoader myClassLoader = MyClass.class.getClassLoader();
Thread.currentThread().setContextClassLoader(myClassLoader);
}  

I had a similar issue and this fixed it. Now this solution doesnt really answer why the problem exists, but I really couldnt find that out.
For reference check this answer.

like image 22
Urban Avatar answered Jan 13 '23 11:01

Urban