问题描述
CustomExceptionHandler
public class CustomExceptionHandler implements UncaughtExceptionHandler {
private Context ctx;
private ContentResolver cr;
public CustomExceptionHandler(Context ctx, ContentResolver cr) {
this.ctx = ctx;
this.cr = cr;
}
public void uncaughtException(Thread t, Throwable e) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
e.printStackTrace(printWriter);
String stacktrace = result.toString();
printWriter.close();
String deviceUuid = Utilities.DeviceUuid(ctx, cr);
String bluetoothName = Utilities.LocalBluetoothName();
AsyncTasks.ErrorLogTask logTask = new AsyncTasks.ErrorLogTask(e, bluetoothName, deviceUuid, stacktrace);
logTask.execute();
}
}
从我的主要活动名为:
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(getBaseContext(), getContentResolver()));
当异常发生,现在,我没有拿到常规不幸的是,已经停止弹出。它只是一个黑色的屏幕。如果我删除从我的主要活动我的电话,换句话说不要使用 CustomExceptionHandler
了,我得到的默认行为。
When an exception occurs now, i dont get the regular "unfortunately, has stopped" popup. Its just a black screen. If i remove my call from my main activity, in other words dont use the CustomExceptionHandler
anymore, i get the default behaviour.
有没有什么办法来实现我的类的默认错误行为?
Is there any way to implement the default error behaviour in my class?
在此先感谢!
推荐答案
您可以添加以下的异常处理程序年底获得不幸的是已停止对话框:
You can add the following at the end of your exception handler to get the "unfortunately has stopped" dialog:
System.exit(1);
然而,这将导致该过程终止,这意味着你的AsyncTask不会运行完成
However, this will cause the process to terminate which means that your AsyncTask will not run to completion.
在任何情况下,我会怀疑你的code将可靠地运行无论如何,如果你是在一个UncaughtExceptionHandler的,因为你不知道你的应用程序的状态。它可能的工作,它可能不会。什么,你也可以尝试是建立在你的UncaughtExceptionHandler一个新线程,拥有该线程睡一小会儿,然后终止使用System.exit()的应用程序。这可能给你的AsyncTask足够的时间来完成运行。
In any case, I would doubt that your code will run reliably anyway if you are in an uncaughtExceptionHandler, because you have no idea what the state of your application is. It might work, and it might not. What you could also try is to create a new Thread in your uncaughtExceptionHandler and have that thread sleep for a little while and then terminate the application using System.exit(). That may give your AsyncTask enough time to run to completion.
这篇关于是,setDefaultUncaughtExceptionHandler使应用程序崩溃默默地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!