eclipse java.lang.RuntimeException: 无法实例化活动 ComponentInfo{…}: java.lang.ClassNotFoundException: 没有找到类

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

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{…}: java.lang.ClassNotFoundException: Didn't find class

javaandroideclipselogcat

提问by user3908478

I am developing an app using Fused Location Provider. I am getting an error "Unfortunately, appname has stopped" when i attempt to run the program. Eclipse doesnt show that there are any mistakes but there are errors in the logcat. I dont know how to fix them. I downloaded source code here. Please help me to understand what I did wrong?

我正在使用 Fused Location Provider 开发应用程序。当我尝试运行该程序时,出现错误“不幸的是,appname 已停止”。Eclipse 不显示有任何错误,但 logcat 中有错误。我不知道如何修复它们。我在这里下载了源代码。请帮我理解我做错了什么?

LogCat

日志猫

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

采纳答案by Jorgesys

Since your exception is:

由于您的例外是:

ClassNotFoundException: Didn't find class "com.kpbird.fusedlocation.MainActivity"

into your AndroidManifest.xmladd the complete package were your activity is located:

AndroidManifest.xml添加到您的活动所在的完整包中:

<activity
                android:name="com.kpbird.fusedlocation.MainActivity"

or be sure to have to correct package name defined:

或者一定要更正定义的包名:

package="com.kpbird.fusedlocation"

I think you typed incorrectly the package of your application in some place of your code!

我认为您在代码的某个位置错误地输入了应用程序的包!

enter image description here

在此处输入图片说明

回答by EmmanuelMess

This happened when I moved an app from one directory to another once, if @Jorgesys's solutiondoesn't help, consider renaming the module and cleaning the project. Note: I was using Android Studio.

当我将应用程序从一个目录移动到另一个目录时发生了这种情况,如果@Jorgesys 的解决方案没有帮助,请考虑重命名模块并清理项目。注意:我使用的是 Android Studio。

回答by AnR

While updating my MainActivity.java I accidentally changed my package name from package com.ppg;to package com.PPG;(upper case instead of lower case). When I changed back to lower case it started working.

在更新我的 MainActivity.java 时,我不小心将包名从 更改package com.ppg;package com.PPG;(大写而不是小写)。当我改回小写时,它开始工作。

回答by Jacob

The same issue occurs while including in AndroidManifestreference with:

AndroidManifest引用以下内容时会发生同样的问题:

  <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

and hardcoded wrong google_play_services_versionvalue.

并硬编码错误google_play_services_version值。

回答by Mehul

After spending much time on this problem i finally got the solution.

在这个问题上花了很多时间后,我终于得到了解决方案。

Just remove setContentView(R.layout.LAYOUT_NAME);from constructor of class and put it into onCreatemethod of the class.

只需setContentView(R.layout.LAYOUT_NAME);从类的构造函数中删除并将其放入onCreate类的方法中。

100% it will be solved.

100%会解决的。