Android Application ClassNotFoundException, Unable

2019-04-26 05:52发布

问题:

This exception is reported 10/week, I tried to find solution but failed. I'm a little frustrated with this sort of exception, and searched all related posts. (P.S. I noticed that full name (android:name="com.example.AcraApplication") throw more exceptions than short one(android:name=".AcraApplication").)

public class AcraApplication extends Application {
public void onCreate() {
        ACRA.init(this);
        super.onCreate();
    }


<application
        android:name=".AcraApplication"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name" >

LogCat:

java.lang.RuntimeException: Unable to instantiate application com.example.AcraApplication: java.lang.ClassNotFoundException: Didn't find class "com.example.AcraApplication" on path: DexPathList[[zip file "/mnt/asec/com.example-2/pkg.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:516)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4554)
at android.app.ActivityThread.access$1600(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.AcraApplication" on path: DexPathList[[zip file "/mnt/asec/com.example-2/pkg.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.app.Instrumentation.newApplication(Instrumentation.java:993)
at android.app.LoadedApk.makeApplication(LoadedApk.java:511)
... 11 more
Suppressed: java.io.IOException: unable to open DEX file
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:296)
at dalvik.system.DexFile.<init>(DexFile.java:80)
at dalvik.system.DexFile.<init>(DexFile.java:59)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:263)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:230)
at dalvik.system.DexPathList.<init>(DexPathList.java:112)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:58)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:65)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:57)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:326)
at android.app.LoadedApk.makeApplication(LoadedApk.java:508)
... 11 more

EDIT: I noticed that when I reinstall&open using ctrl+F11 in eclipse without clicking its icon, it occurs more frequently.

回答1:

Have you declared the package name at the top of your class files?

It should have a package declared at the top of each class file like so:

package com.example;

import ...

public class AcraApplication extends Application {
    ...
}

All of your class files with that package declaration should be in the same directory, src/com/example.

You must also declare your package in your AndroidManifest.xml file as well as all subclasses as <activity ... />

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

    <application
        ... >
        <activity
            android:name=".AcraApplication"
            ... >
        </activity>
    </application>
</manifest>


回答2:

I also had the same error. Solved it by adding below permission in manifest file.

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

Hope this helps. :)