ClassCastException:应用程序对象不是我的自定义应用程序对象 [英] ClassCastException: Application object is not my custom application object

查看:74
本文介绍了ClassCastException:应用程序对象不是我的自定义应用程序对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在Android代码中,我们使用以下模式将数据存储在Application对象中,然后通过强制转换来访问数据:

Often in Android code we use this pattern of storing data in the Application object and then accessing it via casting as such:

((MyApplicationObject)getApplication()).getDaggerComponent()

getApplication( )应该始终为 MyApplicationObject ,因为我已经在 AndroidManifest.xml 中指定了它。但是,当您部署此代码时,有时会导致 ClassCastException ,而我们得到一个不同的对象。

getApplication() should always be MyApplicationObject because I have specified it in the AndroidManifest.xml. However, when you deploy this code, sometimes it causes a ClassCastException and we get a different object instead.

如何

此问题的正确解决方法是什么?

What is the correct workaround for this issue?

推荐答案

在评论中解决:这是一个Android错误,与Dagger无关。

Resolved in the comments: this is an Android bug, not one related to Dagger.


感谢您的链接! 98%的崩溃来自Android 7.0,0.2%来自Android 7.1。在该链接中,一名@google工程师说,他们改进了7.0中的重大问题,但并未完全解决。 – gregm

Thank you for that link! 98% of the crashes are from Android 7.0, with 0.2% from Android 7.1. In the link a @google engineer says they improved things in major issues in 7.0, but didn't completely fix it. – gregm

请参阅此公共Google IssueTracker链接,#37137009:java.lang.ClassCastException:无法将android.app.Application投射到com .xxx.xxApplication #17 中的评论):

See this public Google IssueTracker link, #37137009: java.lang.ClassCastException: Cannot cast android.app.Application to com.xxx.xxApplication (ctate's comment in #17):


是的,这是一个已知问题;并在7.1中得到了解决。从分析中可以看出,7.1中仍然存在一些边缘情况,可能会导致类似的情况-但是机会的范围要窄得多,Android 8也可以解决这些情况。

Yes, this is a known issue; and was largely addressed in 7.1. There are still some edge cases in 7.1 that can result in similar circumstances -- as you can see from your analytics -- but the window of opportunity is much narrower, and Android 8 addressed those as well.

不幸的是,目前似乎没有太多的解释或官方的解决方法,但是至少这个问题主要限于Android 7.0

Unfortunately, for the time being, there doesn't seem to be much of an explanation or official workaround, but at least the issue is predominantly limited in scope to Android 7.0.

这篇关于ClassCastException:应用程序对象不是我的自定义应用程序对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆