Activity 启动时出现 Android ClasscastException
我在 Android 中有一个简单的活动程序。 基本上这个类只是扩展了 Activity。 但是当我启动它时,我在类的构造函数中得到了 ClassCastException 。 我什至没有定义构造函数,所以它必须位于超类 Activity 的构造函数中。
不幸的是,调试器没有提供有关它尝试转换哪个类的任何详细信息。
这是堆栈跟踪:
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread$PackageInfo.makeApplication(boolean, Instrumentation) line: 649
ActivityThread.handleBindApplication(ActivityThread$AppBindData) line: 4232
ActivityThread.access$3000(ActivityThread, ActivityThread$AppBindData) line: 125
ActivityThread$H.handleMessage(Message) line: 2071
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
当我查看运行时异常时,我得到:
detailMessage "Unable to instantiate application com.test.MyApp: java.lang.ClassCastException: com.test.MyApp" (id=830067694464)
唯一的代码是
package com.test;
import android.app.Activity;
public class MyApp extends Activity {
}
i have a simple activity program in android.
basically the class just extends Activity.
But when i start it i get a ClassCastException in the constructor of my class.
i dont even have a constructor defined, so it must be in the constructor of the superclass which is Activity.
unfortunately the debugger doesnt give any detailed information on what class it is trying to cast.
here is the stacktrace:
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread$PackageInfo.makeApplication(boolean, Instrumentation) line: 649
ActivityThread.handleBindApplication(ActivityThread$AppBindData) line: 4232
ActivityThread.access$3000(ActivityThread, ActivityThread$AppBindData) line: 125
ActivityThread$H.handleMessage(Message) line: 2071
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
and when I look into the runtimeexception I get:
detailMessage "Unable to instantiate application com.test.MyApp: java.lang.ClassCastException: com.test.MyApp" (id=830067694464)
the only code is
package com.test;
import android.app.Activity;
public class MyApp extends Activity {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我同意 Mahesh 的答案。这会发生是由于分配视图而不进行强制转换...
如果您将 ImageButton 视图强制转换为按钮视图或其他视图类,也会发生这种情况。
I agree with Mahesh's answer.. This will happen due to assigning the view without casting...
This will also happen if you're casting an ImageButton View to Button View or some other View Class.
我遇到了同样的问题,并且我发现这导致了preferences.xml 中的错误使用
必须改为
I had same problem, and I figure out, that this caused wrong usage in preferences.xml
<CheckBoxPreference android:key="@+id/string" />
had to be change to
<CheckBoxPreference android:key="string" />
有一个类似的问题,ClassCastException 告诉我无法将 EditText 转换为 TextView。我并没有试图这么做。尝试清理项目,但收到几个“无法删除文件...”错误。关闭 Eclipse,重新打开它,清理项目。问题就消失了。显然这并不适用于所有情况,但这是一种可能性。
Had a similar issue with a ClassCastException telling me I couldn't cast an EditText as a TextView. I wasn't trying to. Tried to clean the project but received several 'can't delete file...' errors. Closed Eclipse, reopened it, cleaned the project. The problem went away. Clearly this won't work in every case, but it's one possibility.
AndroidManifest.xml
application
android:name
(如果存在)android:name="android.app .Application"
这就是我所做的,问题就消失了。
PS:在步骤 4 中,使用您的自定义应用程序类名称(如果您有的话)(这是可选的)。
AndroidManifest.xml
application
android:name
(if exists)android:name="android.app.Application"
This is what I did and the problem had gone.
P.S: In Step 4 use your custom application class name if you have one (that's optional).
我也有同样的问题。
就我而言,我有一个扩展应用程序的类,该类在清单文件中作为应用程序标记的属性引用。
当我重构该类时,Eclipse 没有在清单中包含引用(可以理解,但令人沮丧且令人困惑),这导致了此问题。
这也与马克西姆的回答有关,但解释似乎是差异。
当我更改属性名称以匹配我重命名的文件时,问题得到解决。
I had the same problem.
In my case I had a class that extended application which was referenced in the manifest file as an attribute of the application tag.
When I re-factored the class Eclipse didn't include the reference in the manifest (understandably but frustrating and confusingly) which resulted in this issue.
This also ties in with Maxim's answer but the explanation would seem to be the disparity.
When I changed the attribute name to match the file that I renamed the problem was resolved.
我犯了一个愚蠢的错误,
这导致了classcastException..
我将其更改为明显的,
并且它起作用了..
我不知道如何,但我只能说这种愚蠢的错误也会导致以上问题..
i made a silly mistake,
and this caused the classcastexception..
i changed this to the obvious,
and it worked..
i dont know how, but all i can tell is that such silly mistakes can also cause the above problem..
添加此内容并发布完整的 Manifest.xml
add this and post your full Manifest.xml
我偶尔也会遇到同样的问题,有时显然是由对 XML 布局中的项目重新排序而触发的。
如果您允许应用程序在未捕获的异常后继续执行,您应该会发现在“adb logcat”中获得的堆栈跟踪比在调试器停止时轻松退出调试器更有用。
这通常会将您指向异常实际发生的位置,这通常不是异常在调试器中停止的位置。
就我而言,这似乎是资源 ID 之间的不匹配 - 在 XML 文件中移动内容似乎会造成混乱,因此当我执行 view.findViewById(R.id.something) 时,它没有返回正确的对象。我将对象转换为 TextView,根据我的 XML 布局的内容,它应该是完全有效的,但它崩溃了。
您可能会发现实际上有一个错误,您正在执行无效的强制转换,这肯定会导致此异常发生,但在我的情况下,强制清理并重建解决了问题 - 它从头开始重新生成已编译的布局和资源 ID ,一切都按预期进行。
I had the same problem occuring occasionally, sometimes apparently triggered by re-ordering items in an XML layout.
If you allow the app to continue executing after the uncaught exception, you should find that you get a much more helpful stacktrace in "adb logcat" than you can easily get out of the debugger at the point where it stops.
This will usually point you at the location where the exception actually occurred, which is generally not the location where it stops in the debugger.
In my case, it seemed to be a mismatch between resource IDs - moving things around in the XML file seemed to confuse things such that when I did view.findViewById(R.id.something) it wasn't returning me the correct object. I was casting the object to a TextView, which should have been perfectly valid according to the contents of my XML layout, but it was crashing.
You may find that you actually have a bug where you're doing an invalid cast, which will certainly cause this exception to happen, but in my case forcing a clean and rebuild solved the problem - it regenerated the compiled layouts and resource IDs from scratch, and everything worked as expected.
如果使用谷歌地图:
记住这一行。不敢相信我忘了它并浪费了这么多时间。
If using google maps:
Remember this line. Can't believe I forgot it and wasted so much time.