java Android ClassNotFoundException:未在路径上找到类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44042107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:53:53  来源:igfitidea点击:

Android ClassNotFoundException: Didn't find class on path

javaandroid

提问by u6029717

Here's the exception message that I get when I launch the app.

这是我启动应用程序时收到的异常消息。

FATAL EXCEPTION: main Process: net.johnhany.opencv3jniPID: 10721 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{net.johnhany.opencv3jni/net.johnhany.opencv3jni.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "net.johnhany.opencv3jni.MainActivity" on path: DexPathList[[zip file "/data/app/net.johnhany.opencv3jni-1/base.apk"],nativeLibraryDirectories=[/data/app/net.johnhany.opencv3jni-1lib/arm, /vendor/lib, /system/lib]] at android.app.ActivityThread.performLaunchActivityActivityThread.java:2322) at android.app.ActivityThread.handleLaunchActivityActivityThread.java:2474) at android.app.ActivityThread.ess$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessageActivityThread.java:1359) at android.os.Handler.sage(Handler.java:102) at android.os.Looper.loopLooper.java:155) at android.app.ActivityThread.mainActivityThread.java:5696) at java.lang.reflect.Method.invokeNative Method) at java.lang.reflect.Method.invokeMethod.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:1028) at com.android.internal.os.Init.main(ZygoteInit.java:823) Caused by: java.lang.ClassNotFoundException: Didn't find class "net.johnhany.opencv3jni.MainActivity" on path: DexPathList[[zip file "/data/app/net.johnhany.opencv3jni-1/base.apk"],nativeLibraryDirectories=[/data/app/net.johnhany.opencv3jni-1/lib/arm/vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClassBaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClassClassLoader.java:511) at java.lang.ClassLoader.loadClassClassLoader.java:469) at android.app.Instrumentation.newActivityInstrumentation.java:1083) at android.app.ActivityThread.performLaunchActivityActivityThread.java:2312) at android.app.ActivityThread.handleLaunchActivityActivityThread.java:2474)? at android.app.ActivityThread.ess$800(ActivityThread.java:144)? at android.app.ActivityThread$H.handleMessageActivityThread.java:1359)? at android.os.Handler.sage(Handler.java:102)? at android.os.Looper.loopLooper.java:155)? at android.app.ActivityThread.mainActivityThread.java:5696)? at java.lang.reflect.Method.invokeNative Method)? at java.lang.reflect.Method.invokeMethod.java:372)? at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:1028)? at com.android.internal.os.Init.main(ZygoteInit.java:823)? Suppressed: java.lang.ClassNotFoundException: net.johnhany.opencv3jni.ty at java.lang.Class.classForNameNative Method) at java.lang.BootClassLoader.Class(ClassLoader.java:781) at java.lang.BootClassLoader.Class(ClassLoader.java:841) at java.lang.ClassLoader.loadClassClassLoader.java:504) ... 13 more Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available 05-18 16:01:27.927 645-1928/? E/ActivityManager: App crashedProcess: net.johnhany.opencv3jni

469) 在 android.app.Instrumentation.newActivityInstrumentation.java:1083) 在 android.app.ActivityThread.performLaunchActivityThread.java:2312) 在 android.app.ActivityThread.handleLaunchActivityThread.java:2474)?在 android.app.ActivityThread.ess$800(ActivityThread.java:144)?在 android.app.ActivityThread$H.handleMessageActivityThread.java:1359)?在 android.os.Handler.sage(Handler.java:102)?在 android.os.Looper.loopLooper.java:155)?在 android.app.ActivityThread.mainActivityThread.java:5696)?在 java.lang.reflect.Method.invokeNative 方法)?在 java.lang.reflect.Method.invokeMethod.java:372)?在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:1028)?在 com.android.internal.os.Init.main(ZygoteInit.java:823)?抑制:java.lang.ClassNotFoundException:net.johnhany.opencv3jni。ty at java.lang.Class.classForNameNative Method) at java.lang.BootClassLoader.Class(ClassLoader.java:781) at java.lang.BootClassLoader.Class(ClassLoader.java:841) at java.lang.ClassLoader.loadClassClassLoader。 java:504) ... 13 更多 引起:java.lang.NoClassDefFoundError: Class not found using the boot class loader; 没有可用的堆栈 05-18 16:01:27.927 645-1928/? E/ActivityManager: App crashedProcess: net.johnhany.opencv3jni

回答by farhad.kargaran

My problem solved using use multi dex:

我的问题使用 use multi dex 解决了:

android {
defaultConfig {

    // Enabling multidex support.
    multiDexEnabled true
}
...
}
dependencies 
{
compile 'com.android.support:multidex:1.0.0'
}

If you do not override the Application class, edit your manifest file to set android:name in the tag as follows:

如果您不覆盖 Application 类,请编辑您的清单文件以在标签中设置 android:name,如下所示:

<application
        android:name="android.support.multidex.MultiDexApplication" >
    ...
</application>

If you do override the Application class, change it to extend MultiDexApplication (if possible) as follows:

如果您确实覆盖了 Application 类,请将其更改为扩展 MultiDexApplication(如果可能),如下所示:

public class MyApplication extends MultiDexApplication

Or if you do override the Application class but it's not possible to change the base class, then you can instead override the attachBaseContext() method and call MultiDex.install(this) to enable multidex:

或者,如果您确实覆盖了 Application 类但无法更改基类,那么您可以覆盖 attachBaseContext() 方法并调用 MultiDex.install(this) 以启用 multidex:

public class MyApplication extends SomeOtherApplication {
@Override
protected void attachBaseContext(Context base) {
 super.attachBaseContext(base);
 MultiDex.install(this);
}
}

回答by Atul O Holic

I was using Instant Run in Android Studio and same error happened to me because I was installing the debug.apkthat was created inside outputs\apkfolder and it turns to be smaller and with missing classes because it probably was the outcome of an instant run.

我在 Android Studio 中使用 Instant Run,同样的错误发生在我身上,因为我正在安装debug.apkoutputs\apk文件夹内创建的文件,它变得更小并且缺少类,因为它可能是即时运行的结果。

Some pointed on Turning Off instant run but instead of turning instant run off just build the apk by clicking on Build->Build apkin the menu and use that apk for testing.

有些人指出关闭即时运行,而不是关闭即时运行,只需通过单击Build->Build apk菜单中的 来构建 apk并使用该 apk 进行测试。

回答by Mallikarjuna

Make sure this Diable preDexLibraries

确保这个 Diable preDexLibraries

In your appmodule .gradle file

在您的app模块 .gradle 文件中

android {
 dexOptions
            {
                preDexLibraries false
            }

}

回答by venkata krishnan

Usually this error happens, when the application fails to find the launcher activity.

当应用程序无法找到启动器活动时,通常会发生此错误。

Suggestions :

建议:

  1. Check your mainfest file for the right launcher activity

  2. Check your main activity is extending from activity or Appcompatactivity

  3. Clean the project and build it again.
  1. 检查您的 mainfest 文件以获取正确的启动器活动

  2. 检查您的主要活动是否从活动或 Appcompatactivity 扩展

  3. 清理项目并重新构建它。

回答by u6029717

here's my solution: 1.Change the application name in AndroidManifest to full path 2.Clean Project 3.Rebuild Project 4.Build APK

这是我的解决方案: 1.将 AndroidManifest 中的应用程序名称更改为完整路径 2.Clean Project 3.Rebuild Project 4.Build APK

回答by Bajrang Hudda

In my case, I removed apply plugin: 'kotlin-android'from module level gradle file by mistake.

就我而言,我错误地从模块级 gradle 文件中删除了apply plugin: 'kotlin-android'

After putting it again, it starts working again.

再次放置后,它再次开始工作。

回答by Bobby.jdk

I had this problem once and clearly remember:

我曾经遇到过这个问题,并且清楚地记得:

My Mistake
I created two projects with same name in different folders.

我的错误
我在不同的文件夹中创建了两个同名的项目。

My Solution
On discovering this i deleted the older project and then :
1.Clean Project
2.Rebuild
3.Run

我的解决方案
在发现这个我删除旧的项目,然后:
1.Clean项目
2.Rebuild
3.Run