Android ClassNotFoundException:未在路径上找到类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20924004/
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-08-20 03:42:10  来源:igfitidea点击:

Android ClassNotFoundException: Didn't find class on path

androidclassnotfoundexception

提问by user3129072

I can't find the solution of this error.Can you please give the permanent solution.i have no idea how to solve it can u help me .Thanks

我找不到此错误的解决方案。请您提供永久解决方案。我不知道如何解决,您能帮我吗?谢谢

01-04 11:06:42.302: E/AndroidRuntime(1906): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{e.gochat/e.gochat.gochat.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "e.gochat.gochat.MainActivity" on path: DexPathList[[zip file "/data/app/e.gochat-1.apk"],nativeLibraryDirectories=[/data/app-lib/e.gochat-1, /vendor/lib, /system/lib]]
01-04 11:06:42.302: E/AndroidRuntime(1906):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at android.app.ActivityThread.access0(ActivityThread.java:135)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at android.os.Looper.loop(Looper.java:137)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at android.app.ActivityThread.main(ActivityThread.java:4998)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at java.lang.reflect.Method.invokeNative(Native Method)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at java.lang.reflect.Method.invoke(Method.java:515)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at dalvik.system.NativeStart.main(Native Method)
01-04 11:06:42.302: E/AndroidRuntime(1906): Caused by: java.lang.ClassNotFoundException: Didn't find class "e.gochat.gochat.MainActivity" on path: DexPathList[[zip file "/data/app/e.gochat-1.apk"],nativeLibraryDirectories=[/data/app-lib/e.gochat-1, /vendor/lib, /system/lib]]
01-04 11:06:42.302: E/AndroidRuntime(1906):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
01-04 11:06:42.302: E/AndroidRuntime(1906):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
01-04 11:06:42.302: E/AndroidRuntime(1906):     ... 11 more
01-04 11:07:06.772: I/Process(1906): Sending signal. PID: 1906 SIG: 9

This is my app Manifest XML

这是我的应用程序清单 XML

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="e.gochat"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="18" />
<permission
    android:name="e.gochat.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="e.gochat.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
    android:name="e.gochat.Common"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >


    <activity
        android:name="e.gochat.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <activity android:name="e.gochat.ChatActivity" 
        />
    <activity
        android:name="e.gochat.SettingsActivity"
        android:label="@string/title_activity_settings" />
    <receiver
        android:name="e.gochat.client.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />

            <category android:name="e.gochat" />
        </intent-filter>
    </receiver>
    <provider
        android:name="e.gochat.DataProvider"
        android:authorities="e.gochat.provider" 
        android:exported="false">
    </provider>


</application>

I'm not sure what's wrong here. I've added the .jar file in the java build path. And yet I'm getting this classnotfound exception. The app loads when I import the android.app.Application

我不确定这里出了什么问题。我在 java 构建路径中添加了 .jar 文件。然而,我收到了这个 classnotfound 异常。当我导入 android.app.Application 时应用程序加载

采纳答案by ramaral

I don't know for sure what is your package name, but if it is e.gochatdo this.

我不确定你的包名是e.gochat什么,但如果是这样。

Add package="e.gochat"to manifest.xml

添加package="e.gochat"到 manifest.xml

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

On each activity declaration for android:nameput a dot before activity name:

在每个活动声明上,android:name在活动名称前放一个点:

<activity
    android:name=".MainActivity">
............
</activity>

<activity 
    android:name=".ChatActivity">

EDIT

编辑

After your edited I can see e.gochatis your package name.
Now you only need to remove e.gochatfrom each activity/provider/receiver name as I said in second part of my answer.
Android will add that for you.

在您编辑后,我可以看到e.gochat您的包名称。
现在,您只需e.gochat要从每个活动/提供者/接收者的名称中删除,就像我在回答的第二部分中所说的那样。
Android 会为您添加。

回答by Kishor N R

I have solved this problem by disabling the instant run option of android studio. To disable Instant Run Goto File -> Settings -> Build,Execution, Deployment -> Instant Run -> Uncheck the checkbox for instant run

我通过禁用 android studio 的即时运行选项解决了这个问题。禁用即时运行转到文件 -> 设置 -> 构建、执行、部署 -> 即时运行 -> 取消选中即时运行的复选框

回答by styler1972

Using Android Studio

使用 Android Studio

After upgrading to Android Stuido 0.8.2, I had this problem. I tried a lot of things, including

升级到Android Stuido 0.8.2 后,我遇到了这个问题。我尝试了很多东西,包括

  • Clean project
  • Clean project (gradle clean)
  • Invalidate Caches and Restart
  • De-qualify the Activity Name in AndroidManifest.xml (.MyActivity)
  • 清洁工程
  • 清洁项目(gradle clean)
  • 使缓存无效并重新启动
  • 取消限定 AndroidManifest.xml (.MyActivity) 中的活动名称

Nothing worked until I re-imported the entire project.

在我重新导入整个项目之前没有任何效果。

File -> Close Project -> Import Project... -> Choose the same project -> OK

File -> Close Project -> Import Project... -> Choose the same project -> OK

This fixed it for me

这为我修好了

回答by live-love

Uninstalling the app in the device and running the project again fixed it for me.

卸载设备中的应用程序并再次运行该项目为我修复了它。

回答by Tom Redman

For me, this was occurring on devices running < Android 5.0 because I didn't have multidex support enabled(this is, over 65k total methods incl. libs). Multidex is automatic and native on Android 5.0+ (specifically, devices using ART).

对我来说,这发生在运行 < Android 5.0 的设备上,因为我没有启用 multidex 支持(这是,包括库在内的总共超过 65k 的方法)。Multidex 在 Android 5.0+(特别是使用ART 的设备)上是自动和原生的。

回答by Himadri Pant

The reason why this was affecting me was due to an incorrect path name in a layout file

这影响我的原因是布局文件中的路径名不正确

<android.support.v4.app.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> 

</android.support.v4.app.view.ViewPager>

The correct absolute path of ViewPager is

ViewPager的正确绝对路径是

android.support.v4.view.ViewPager

After correcting the path it worked. I did not have to prefix the activity name with a '.'.

更正路径后,它起作用了。我不必在活动名称前加上“.”。

回答by Kushal

The problem was solved by removing MultiDexSupport from build.gradlefile :

通过从build.gradle文件中删除 MultiDexSupport 解决了这个问题:

defaultConfig {
    multiDexEnabled true    // This line was causing above problem
}

Removing the MultiDexSupport solved the problem

删除 MultiDexSupport 解决了问题

回答by Amit

This issue has multiple answers based on the different scenario. I was getting the same error when using a library(.aar) in my Android Studio project.

这个问题根据不同的场景有多个答案。在我的 Android Studio 项目中使用库 (.aar) 时,我遇到了同样的错误。

The root cause in my case was that I missed to add a third party lib dependency in app's build.gradle file which was being used by the .aar file.

在我的案例中,根本原因是我错过了在应用程序的 build.gradle 文件中添加第三方库依赖项,该文件正被 .aar 文件使用。

e.g:- The library(.aar) was using 'com.squareup.okhttp3:okhttp:3.4.1'. So I had to add

例如:- 库(.aar)使用的是“com.squareup.okhttp3:okhttp:3.4.1”。所以我不得不补充

compile 'com.squareup.okhttp3:okhttp:3.4.1'

编译'com.squareup.okhttp3:okhttp:3.4.1'

in the app's build.gradle. Hope it helps someone.

在应用程序的 build.gradle 中。希望它可以帮助某人。

回答by Zuop

ANDROID STUDIOIn addittion to Kishor's answer:

ANDROID STUDIO除了 Kishor 的回答:

The error occured on my small project when I enabled multiDexEnabled = truein the gradle defaultconfig. Using the generated .apk file crashes on start of the app with the known error message.

当我multiDexEnabled = true在 gradle defaultconfig 中启用时,我的小项目发生了错误。使用生成的 .apk 文件在应用程序启动时崩溃并显示已知错误消息。

For me it was solved when I built the .apk file via gradle (right side of android studio) -> "yourapp" -> Tasks- > install -> installDebug

对我来说,当我通过 gradle(android studio 的右侧)->“yourapp”-> 任务-> 安装-> installDebug 构建 .apk 文件时它就解决了

Hope this helps someone with this strange error

希望这可以帮助遇到这个奇怪错误的人

回答by prom85

I implemented a listener that was not available in the android version I was running yet and this led to the same error.

我实现了一个在我运行的 android 版本中不可用的侦听器,这导致了同样的错误。

Example MainActivity:

示例主活动:

// RecyclerView.OnScrollChangeListener is only available on API >= 23
public class MainActivity implements RecyclerView.OnScrollChangeListener
{

}

Running this activity on a device with API < 23 will result in this error as well...

在 API < 23 的设备上运行此活动也会导致此错误...