Android 工作应用程序上的 java.lang.ClassNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3781151/
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
java.lang.ClassNotFoundException on working app
提问by Skoky
I have created and published my first Android app. It's very simple. It works fine on simulator and some phones, but I am getting this error:
我已经创建并发布了我的第一个 Android 应用程序。这很简单。它在模拟器和某些手机上运行良好,但出现此错误:
java.lang.RuntimeException: Unable to instantiate application cz.teamnovak.droid.Novak ESC Track guide: java.lang.ClassNotFoundException: cz.teamnovak.droid.Novak ESC Track guide in loader dalvik.system.PathClassLoader[/data/app/cz.teamnovak.droid-1.apk]
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:649)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4232)
at android.app.ActivityThread.access00(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: cz.teamnovak.droid.Novak ESC Track guide in loader dalvik.system.PathClassLoader[/data/app/cz.teamnovak.droid-1.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at android.app.Instrumentation.newApplication(Instrumentation.java:942)
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:644)
... 11 more
Any idea what can cause this?
知道什么会导致这种情况吗?
回答by phreakhead
Yep, I had this exact same problem. It was because I specified the android:name
attribute in the application node in the manifest file.
是的,我遇到了完全相同的问题。这是因为我android:name
在清单文件中的应用程序节点中指定了该属性。
Your Android Manifest file probably looks something like this:
您的 Android 清单文件可能如下所示:
<application
android:name="Novak ESC Track guide"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:description="@string/help_text" >
Do not use the android:name
attribute! unless you've implemented a custom Application object.
不要使用android:name
属性!除非您已经实现了自定义 Application 对象。
The application:name
attribute has nothing to do with the name of your app. This is the name of a specific class to load as your Application
instance. That's why you would get the ClassNotFoundException
if that class wouldn't exist.
该application:name
属性与您的应用程序名称无关。这是要作为Application
实例加载的特定类的名称。这就是为什么ClassNotFoundException
如果那个类不存在你会得到的原因。
For the name of the app use the android:label
attribute on this same application node instead.
对于应用程序的名称,请改用android:label
同一应用程序节点上的属性。
Remove it and it should work:
删除它,它应该可以工作:
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:description="@string/help_text" >
回答by hobbs
Something like this happened when I changed the build target to 3.2. After digging around I found that a had named the jar lib folder "lib" instead of "libs". I just renamed it to libs and updated the references on the Java build path and everything was working again. Maybe this will help someone...
当我将构建目标更改为 3.2 时,发生了类似的事情。在挖掘之后,我发现 a 将 jar lib 文件夹命名为“lib”而不是“libs”。我只是将它重命名为 libs 并更新了 Java 构建路径上的引用,一切又恢复了。也许这会帮助某人......
回答by Alex
We have a couple of projects where this issue was logged from time to time on the Android Market. I found the following issues in the manifests:
我们有几个项目不时在 Android Market 上记录此问题。我在清单中发现了以下问题:
If the package name is com.test then activities names should be
.ActivityName
(with a leading dot), not justActivityName
.For some classes, those that appeared in the logs most often, the class name was specified as
com.test.Name
while it should have been.Name
.
如果包名称是 com.test 那么活动名称应该是
.ActivityName
(带前导点),而不仅仅是ActivityName
.对于某些类,那些最常出现在日志中的类,类名被指定为
com.test.Name
应该是.Name
。
I guess many implementations of Android handle these minor issues successfully (this is why the exception never happened in testing), while others few are throwing the exception.
我猜 Android 的许多实现都成功地处理了这些小问题(这就是为什么在测试中从未发生异常的原因),而其他很少有人抛出异常。
回答by Mr G
Had the same error: java.lang.RuntimeException: Unable to instantiate activity (classnotfound) FIRST try to change the build platform (2.3.3 -> 2.2 -> 2.3.3) worked for me.
有同样的错误:java.lang.RuntimeException:无法实例化活动(classnotfound)首先尝试更改为我工作的构建平台(2.3.3 -> 2.2 -> 2.3.3)。
回答by lakshman
This is my observation with respect to the Error. I recently Updated the ADT to 22.0.1. I am getting following Error when i imported my previous Projects "E/AndroidRuntime(24807): Caused by: java.lang.ClassNotFoundException: com.sherl.sherlockfragmentsapp.StartActivity in loader dalvik.system.PathClassLoader[/data/app/com.sherl.sherlockfragmentsapp-1.apk]"
这是我对错误的观察。我最近将 ADT 更新到 22.0.1。当我导入以前的项目 “E/AndroidRuntime(24807): Caused by: java.lang.ClassNotFoundException: com.sherl.sherlockfragmentsapp.StartActivity in loader dalvik.system.PathClassLoader[/data/app/com. sherl.sherlockfragmentsapp-1.apk]"
Then I changed "Properties->Java Build Path-> Order and Export" in the following manner [Unable to add the Image because of the Forum rules]
然后我按照以下方式更改了“属性-> Java构建路径->顺序和导出”[由于论坛规则,无法添加图像]
- Android Private Libraries - checked
- Android 4.2.2 - unchecked
- Android Dependencies - checked
- /src - selected
- /gen - selected
- Android 私有库 - 选中
- Android 4.2.2 - 未选中
- Android 依赖项 - 已检查
- /src - 选中
- /gen - 选中
It resolved the issue. Hope this is Help you guys.
它解决了这个问题。希望这对你们有帮助。
回答by RightHandedMonkey
I have this problem sometimes with eclipse. What has corrected it for me is to go to Project Properties / Android and change the build target API to a different version and republish. I'll find that corrected it, then I can change it back to the desired build target.
我有时会遇到这个问题。对我来说更正的是转到项目属性/Android 并将构建目标 API 更改为不同的版本并重新发布。我会发现它已更正,然后我可以将其更改回所需的构建目标。
or
或者
You may need to check your proguard.cfg.
您可能需要检查您的proguard.cfg。
Assuming you have linked your libraries properly and that your library projects have the code you need marked for export, the next step you might want to do is to check your proguard settings and make sure you are not stripping out the classes you need.
假设您已正确链接您的库并且您的库项目具有您需要标记为导出的代码,您可能想要做的下一步是检查您的 proguard 设置并确保您没有删除您需要的类。
I was struggling with this quite a bit after I had my app working going directly to the emulator or device from eclipse. The problem I was having was after the app was published (i.e. gone through proguard) and run on the device it was missing classes that were contained in the project. They were being stripped out somehow.
在我的应用程序直接从 eclipse 运行到模拟器或设备后,我为此苦苦挣扎。我遇到的问题是在应用程序发布(即通过 proguard)并在设备上运行后,它缺少项目中包含的类。他们不知何故被剥夺了。
My problem may have been caused when I had tried to use IntelliJ and have switched back to eclipse.
我的问题可能是在我尝试使用 IntelliJ 并切换回 eclipse 时引起的。
Here is the proguard file that worked for me:
这是对我有用的 proguard 文件:
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keepclasseswithmembers class * {
native <methods>;
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
回答by Logan
I got this error when I ran my app on earlier versions of android. I thought SearchViewwas backwards compatible to Android 1.5, but it was created in 3.0. I removed its reference from the code and it worked.
当我在早期版本的 android 上运行我的应用程序时出现此错误。我认为SearchView向后兼容 Android 1.5,但它是在 3.0 中创建的。我从代码中删除了它的引用并且它起作用了。
回答by icyerasor
Had this sort of problem today after upgrading to latest ADT/SDK. Took me quite a while. Checked that i used google-apis (for maps), uses-library, cleaned the project etc.
升级到最新的 ADT/SDK 后今天遇到了这种问题。花了我很长时间。检查我是否使用了 google-apis(用于地图)、uses-library、清理项目等。
Deleting the .project and adding a fresh one (create new android project) finally solved it.
删除 .project 并添加一个新的(创建新的 android 项目)终于解决了它。
回答by Joel Martinez
I know this question has been answered, and it likely wasn't the case. But I was getting this error, and figured I'd post why in case it can be helpful to anyone else.
我知道这个问题已经得到了回答,但情况可能并非如此。但是我遇到了这个错误,我想我会发布原因,以防它对其他人有帮助。
So I was getting this error, and after several hours sheepishly realized that I had unchecked 'Project > Build Automatically'. So although I had no compilation errors, this is why I was getting this error. Everything started working as soon as I realized that I wasn't actually building the project before deploying :-/
所以我收到了这个错误,几个小时后羞怯地意识到我没有选中“项目>自动构建”。因此,虽然我没有编译错误,但这就是我收到此错误的原因。当我意识到我实际上并没有在部署之前构建项目时,一切都开始工作了:-/
Well, that's my story :-)
好吧,这就是我的故事:-)
回答by lost_bits1110
Make sure that android:hasCode
is not set to false
in your manifest file. This is what solved the problem for me!
确保android:hasCode
未false
在清单文件中设置为。这就是为我解决问题的原因!