实例化 AlertDialog 的 Android UncaughtExceptionHandler 中断
正如标题所示,我在自定义应用程序类中设置了一个 UncaughtExceptionHandler
,当我的应用程序启动时,它被设置为默认值。
但是,虽然文档中没有,但我读到您无法使用 MyApplication.this
或 getApplicationContext()
实例化 Dialog
。当我尝试这样做时,我得到了
<块引用>WindowManager$BadTokenException...
我需要一个替代方案。我曾想过创建一个新的活动来弥补这一点,但宁愿让它成为一个 AlertDialog
。
有没有办法从 MyApplication
类中获取我的 UncaughtExceptionHandler
的有效上下文?
As the title says, I have an UncaughtExceptionHandler
set in my custom application class that gets set as the default when my application starts.
However, while not in the documentation I have read that you cannot instantiate a Dialog
using MyApplication.this
or getApplicationContext()
. When I try to do it, I get a
WindowManager$BadTokenException...
I need an alternative. I have thought of making a new activity to compensate for this but would rather have it be an AlertDialog
.
Is there a way I can get a valid context to my UncaughtExceptionHandler
from inside MyApplication
class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确。应用程序上下文对于构建 UI 并不是特别有用。
在这种情况下,
AlertDialog
是不可能的。对于初学者来说,只有一个活动可以打开一个对话框,并且您的任何活动都可能没有正在运行(例如,在后台服务中崩溃)和有效(例如,您有一个活动,但它是崩溃的)。编辑:修正了上一段以消除混乱
Correct. The Application context is not especially useful for constructing UIs.
An
AlertDialog
is impossible in this case. For starters, only an activity can open a dialog, and none of your activities may be running (e.g., crash in a background service) and valid (e.g., you had one, but it's the one that crashed).EDIT: Fixed previous paragraph to clear up confusion