4

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)
naradlov
  • 107
  • 1
  • 9
  • Tell us a bit more about your two PCs setup: 1) are they using the same version of the andriod sdk? 2) Have you made sure that some other Android project will run fine on your new machine? 3) Is it just plain andriod project or it depends on Android library project of yours? 4) Does it depend on 3rd party libraries and are they the same on both machines? 5) Also make sure all versions on the two machines are the same: either 32 bit or 64 bit distributions. – Boris Strandjev Apr 22 '12 at 07:23
  • The machine it currently works on was running an older version of eclipse and ADT(15.00) . Once I updated eclipse and the ADT to the latest on the old machine, it now doesn't work on both machines. I was able to run a VERY simple test program on both machines, but not my actual project. One machine is 64 bit, other is 32 bit. But considering they both don't work now, I believe it is because of either an update to Eclipse or the ADT. Although I am not sure what that could be. – naradlov Apr 22 '12 at 18:47
  • I just ran a test where I removed everything in the manifest.xml and replaced it with the VERY simple working project, and pointed it to a dumby activity and it still gives this error. Is there some configuration that is bad somewhere? I've tried rebuilding the .project and .classpath – naradlov Apr 22 '12 at 18:53
  • Well, one advise will be to start and build up your application bit by bit and try to see what causes the problem. Hopefully it is not that awefully complex and it will not take you that long... – Boris Strandjev Apr 22 '12 at 21:23
  • 1
    That is exactly what I did, built it piece by piece. Didn't change anything I could tell and it worked. So maybe it had something to do with using an older ADT and eclipse and bringing it to a new one. Either way, it is working fine now. Thanks everyone for their help. – naradlov Apr 23 '12 at 17:43

4 Answers4

8

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.
user2503713
  • 96
  • 1
  • 2
  • I had the same issue and this did the trick for me. Still curious what changed since 2 days ago, but its fixed now, so I'm happy. – invertigo Jun 21 '13 at 16:06
  • Yep, confirmed. This is ways more elegant solution than messing code with classloader everywhere – Y.A.P. Jun 26 '13 at 08:33
2

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.

Community
  • 1
  • 1
Urban
  • 2,201
  • 24
  • 52
  • So I added this, public PopActivity() { fixClassLoaderIssue(); } Didn't seem to fix the issue? – naradlov Apr 21 '12 at 23:16
  • Hmm..that shouldve fixed it. Can you try to make sure if your constructor is called by adding a log statement in it? It most probably will be called, but just to make sure. – Urban Apr 21 '12 at 23:23
  • I added the log before the fixClassLoaderIssue() and it doesn't display the log. The Stack says it is caused by a ClassNotFoundException, so the class is never actually called? – naradlov Apr 21 '12 at 23:26
  • Thats quite odd. Did you try to clean the project and rebuilding it? Go to Project menu option and select 'clean'. – Urban Apr 21 '12 at 23:32
  • only about 100 times, :) This code works perfectly fine on my other machine, this is a released app that runs fine on every device. Just this new comp I built gives me this error. – naradlov Apr 21 '12 at 23:36
  • Well, I cant really figure out this. Sorry for that. Eclipse does become very troublesome quite often. I had to struggle with classNotFound for a long time before I found the fix I posted above. I'll try looking for something better. – Urban Apr 21 '12 at 23:41
0

Did you try a project clean? That usually solves my classloader issues.

SeanPONeil
  • 3,901
  • 4
  • 29
  • 42
0

In my case It was wrong package name in AndroidManifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    *package="com.app"*
    android:versionCode="1"
    android:versionName="1.0" >
dmv
  • 165
  • 1
  • 6