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

时光毁灭记忆、已成空白 提交于 2019-12-07 09:11:57

问题


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)

回答1:


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.



回答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.




回答3:


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




回答4:


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" >


来源:https://stackoverflow.com/questions/10263248/android-unable-to-instantiate-activity-classnotfound-fails-on-one-eclipse

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