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

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

Android ClassNotFoundException: Didn't find class on path

javaandroideclipse

提问by Phito

10-22 15:29:40.897: E/AndroidRuntime(2561): FATAL EXCEPTION: main
10-22 15:29:40.897: E/AndroidRuntime(2561): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gvg.simid/com.gvg.simid.Login}: java.lang.ClassNotFoundException: Didn't find class "com.gvg.simid.Login" on path: DexPathList[[zip file "/data/app/com.gvg.simid-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.gvg.simid-1, /vendor/lib, /system/lib]]
10-22 15:29:40.897: E/AndroidRuntime(2561):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at android.app.ActivityThread.access0(ActivityThread.java:141)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at android.os.Looper.loop(Looper.java:137)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at java.lang.reflect.Method.invokeNative(Native Method)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at java.lang.reflect.Method.invoke(Method.java:525)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at dalvik.system.NativeStart.main(Native Method)
10-22 15:29:40.897: E/AndroidRuntime(2561): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.gvg.simid.Login" on path: DexPathList[[zip file "/data/app/com.gvg.simid-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.gvg.simid-1, /vendor/lib, /system/lib]]
10-22 15:29:40.897: E/AndroidRuntime(2561):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-22 15:29:40.897: E/AndroidRuntime(2561):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
10-22 15:29:40.897: E/AndroidRuntime(2561):     ... 11 more

I'm not really sure what is causing it as it is correctly listed in the manifest:

我不确定是什么原因造成的,因为它在清单中正确列出:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gvg.simid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.READ_OWNER_DATA" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_OWNER_DATA" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <uses-feature android:name="android.hardware.usb.host" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gvg.simid.Login"
            android:label="@string/app_name" 
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I also added "Android Private Libraries" to build path, and moved it to the top of "Order and Export", but it still gave me the same error.

我还添加了“Android Private Libraries”来构建路径,并将其移动到“Order and Export”的顶部,但它仍然给了我同样的错误。

EDIT: I rebuilt the project completely, and was unable to reproduce the error. Not sure what was causing it.

编辑:我完全重建了项目,但无法重现错误。不知道是什么原因造成的。

回答by Danilo Silva

If you are using Eclipse try Project -> Clean

如果您使用的是 Eclipse,请尝试 Project -> Clean

回答by Chris

This following method worked for me

以下方法对我有用

  • Right click on your projectand select properties
  • The "Properties for " panel will open. From the menu on the left go to Java Build Path-> Order and Export
  • From the list below uncheck the box next to "Android Dependencies"
  • Finally clean your projectand run
  • 右键单击您的project并选择properties
  • “属性”面板将打开。从左侧菜单转到Java Build Path->Order and Export
  • 从下面的列表中取消选中旁边的框 "Android Dependencies"
  • 最后clean your project运行

回答by pioneerBhawna

This happens if we change Build Path of the APP, this can be in any case of Adding or Removing or Changing Libraries or .jar file. The Best solution is to Restart the Eclipse.

如果我们更改 APP 的构建路径,就会发生这种情况,这可能发生在添加或删除或更改库或 .jar 文件的任何情况下。最好的解决方案是重新启动 Eclipse。

回答by Jorgesys

I had this issue, raised by several causes, but i see this in your stacktrace "**Unable to instantiate activity ComponentInfo{...}: java.lang.ClassNotFoundException: Didn't find class "..." on path: DexPathList[[**", I found a diference in my .classpathbefore it stop working.

我遇到了这个问题,由多种原因引起,但我在您的堆栈跟踪“ **Unable to instantiate activity ComponentInfo{...}: java.lang.ClassNotFoundException: Didn't find class "..." on path: DexPathList[[**”中看到了这一点,.classpath在它停止工作之前,我发现了我的差异。

 <?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
    <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
    <classpathentry kind="src" path="gen"/>
    <classpathentry kind="output" path="bin/classes"/>
</classpath>

I've ADDED the line:

我添加了以下行:

<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>

this is the final version:

这是最终版本:

  <?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
    <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
    <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
    <classpathentry kind="src" path="gen"/>
    <classpathentry kind="output" path="bin/classes"/>
</classpath>

Now it works! =)

现在它起作用了!=)

回答by weiweia

I met the same problem and I noticed the sample code was put under directory "java" instead of directory "src". After moving the code to src, the problem was solved.

我遇到了同样的问题,我注意到示例代码放在目录“java”而不是目录“src”下。将代码移至src后,问题解决。

Also make sure the app directory is split into multiple folders. e.g. /main/com/site/appname/not /main/com.site.appname/

还要确保将应用程序目录拆分为多个文件夹。例如/main/com/site/appname/不是/main/com.site.appname/

Hope it works~

希望有用~

回答by Balaji Dhanasekar

I had the same issue for my project. It happened due to the conflict in android support library version between my project and the library project that I added in my project. Put the same version android support library in your project and library projects you included and clean build... Everything will work...

我的项目遇到了同样的问题。发生这种情况是由于我的项目和我在项目中添加的库项目之间的 android 支持库版本冲突。将相同版本的 android 支持库放在您的项目和您包含的库项目中并清理构建......一切都会正常......

回答by bran.io

I had the same exact issue. As @weiweia suggested, move the code from the java directory to src worked for me. I also removed the Android Dependencies from Project --> Properties --> Java Build Path --> Order and Export, and made sure only to include Library Projects in the Android screen.

我有同样的问题。正如@weiweia 建议的那样,将代码从 java 目录移动到 src 对我有用。我还从项目 --> 属性 --> Java 构建路径 --> 订单和导出中删除了 Android 依赖项,并确保仅在 Android 屏幕中包含库项目。

回答by GoranZarkovic

I had this issue recently and I am using Android Studio 0.8.4

我最近遇到了这个问题,我使用的是 Android Studio 0.8.4

The problem was the fact that I decided to rename an XML menu layout file that was called main.xml.

问题是我决定重命名一个名为 main.xml 的 XML 菜单布局文件。

Right click on it and chose Refactor --> Rename. Before renaming it, be sure uncheck "Search in comments and strings"and pressed Refactor.

右键单击它并选择Refactor --> Rename。在重命名之前,请务必取消选中“在注释和字符串中搜索”并按下Refactor

Because my file was originally named 'main', it renamed critical paths in the app.iml and build.gradle, mainly the java.srcDirs in the build.gradle, since the path is defined as 'src/main/java'.

因为我的文件最初命名为'main',它重命名了 app.iml 和 build.gradle 中的关键路径,主要是 build.gradle 中的 java.srcDirs,因为路径定义为'src/main/java'.

I hope this helps anyone that may be experiencing this issue.

我希望这可以帮助任何可能遇到此问题的人。

回答by user3906863

I had this problem for quite a while, and like everybody else the answers above didn't apply to my project.

我有这个问题已经有一段时间了,和其他人一样,上面的答案不适用于我的项目。

In my project I had linked up a project to my project and it was throwing ClassDefNotFoundError every time some code for the other project was executed.

在我的项目中,我将一个项目链接到我的项目,并且每次执行另一个项目的某些代码时它都会抛出 ClassDefNotFoundError。

So this was my solution. I went to project properties of my project and Java Build Path. Pressed the "Source"-tab and "link source" from src-folder of the other project to my own project and named a new folder "core-src".

所以这是我的解决方案。我去了我的项目和 Java Build Path 的项目属性。将另一个项目的 src 文件夹中的“源”选项卡和“链接源”按到我自己的项目中,并命名一个新文件夹“core-src”。

Hopes this solution helps someone

希望这个解决方案可以帮助某人

回答by PeterH

I had the same issue. I am using Xamarin.Android.

我遇到过同样的问题。我正在使用 Xamarin.Android。

The problem in my case was that I changed the versionCode and versionName.

我的问题是我更改了 versionCode 和 versionName。

I had no problem when I had them set like this:

当我把它们设置成这样时,我没有问题:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="pac.ka.ge" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">

The issue appeared when I changed it to this (versionCode 2 and versionName 0.0.1):

当我将其更改为此(versionCode 2 和 versionName 0.0.1)时出现问题:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package=" pac.ka.ge " android:versionCode="2" android:versionName="0.0.1" android:installLocation="auto">

I fixed the issue by changing to versionCode 2 and versionName 1.0.0.1, like so:

我通过更改为 versionCode 2 和 versionName 1.0.0.1 解决了这个问题,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package=" pac.ka.ge " android:versionCode="2" android:versionName="1.0.0.1" android:installLocation="auto">

I am unsure why this was a problem, maybe Android doesn't like a lower versionName with a higher versionCode?

我不确定为什么这是一个问题,也许 Android 不喜欢较低的 versionName 和较高的 versionCode?

I found the following on http://developer.android.com/guide/topics/manifest/manifest-element.html#vname

我在http://developer.android.com/guide/topics/manifest/manifest-element.html#vname上找到了以下内容

android:versionCode An internal version number. This number is used only to determine whether one version is more recent than another, with higher numbers indicating more recent versions. This is not the version number shown to users; that number is set by the versionName attribute. The value must be set as an integer, such as "100". You can define it however you want, as long as each successive version has a higher number. For example, it could be a build number. Or you could translate a version number in "x.y" format to an integer by encoding the "x" and "y" separately in the lower and upper 16 bits. Or you could simply increase the number by one each time a new version is released.

android:versionName The version number shown to users. This attribute can be set as a raw string or as a reference to a string resource. The string has no other purpose than to be displayed to users. The versionCode attribute holds the significant version number used internally.

android:versionCode 内部版本号。此数字仅用于确定一个版本是否比另一个版本更新,数字越大表示版本越新。这不是向用户显示的版本号;该数字由 versionName 属性设置。该值必须设置为整数,例如“100”。您可以根据需要定义它,只要每个后续版本都有更高的编号。例如,它可以是内部版本号。或者,您可以通过将“x”和“y”分别编码为低 16 位和高 16 位,将“xy”格式的版本号转换为整数。或者,您可以在每次发布新版本时简单地将数字增加 1。

android:versionName 向用户显示的版本号。此属性可以设置为原始字符串或对字符串资源的引用。除了向用户显示之外,该字符串没有其他用途。versionCode 属性包含内部使用的重要版本号。