线程遇到未捕获的异常后,如何显示对话框? [英] How do I show a dialog after the Thread has encountered an uncaught exception?
问题描述
我想检测我的Android应用中何时发生未捕获的异常
。
I want to detect when an uncaught exception
has happened in my Android app.
一旦检测到,我想显示一个确认对话框
Once detected, I want to display a confirmation dialog
如何显示此确认对话框?当我尝试了各种技术时,UI无响应并且似乎被冻结。
How do I get this confirmation dialog to display? When I tried various techniques, the UI is unresponsive and appears to be frozen.
我的代码对此做出了响应:
My code responds to this:
new CatchAllExceptionHandler(this) is my custom handler
Thread.setDefaultUncaughtExceptionHandler(new CatchAllExceptionHandler(this));
我已经尝试了 CatchAllExceptionHandler
的这两种实现:
I have tried these two implementations of CatchAllExceptionHandler
:
- 显示警报对话框
- 启动活动,然后在onCreate <之后显示警报对话框
这两种方法均失败。
所以我的问题是:如何使确认对话框正确显示?
So my question is: How to I get the confirmation dialog to properly display?
推荐答案
您可能正在尝试在非UI线程上执行UI操作。使用任何可用的技术( Handler
, Handler#post
, View#post
, Activity#runOnUiThread
, AsyncTask#onPostExecute
)来安排您的UI工作UI线程。
You may be attempting to do your UI operations on a non-UI thread. Use any of the available techniques (Handler
, Handler#post
, View#post
, Activity#runOnUiThread
, AsyncTask#onPostExecute
) to arrange for your UI work to be done on the UI thread.
这篇关于线程遇到未捕获的异常后,如何显示对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!