gpt4 book ai didi

android - 在 `Thread.setDefaultUncaughtExceptionHandler` 中显示一个对话框

转载 作者:IT老高 更新时间:2023-10-28 23:39:50 28 4
gpt4 key购买 nike

当我的 android 应用程序抛出异常时,我想显示一个自定义对话框来告诉用户发生了错误,所以我使用 Thread.setDefaultUncaughtExceptionHandler 来设置全局异常处理程序:

public class MyApplication extends Application {

@Override
public void onCreate() {
super.onCreate();

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, final Throwable ex) {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("There is something wrong")
.setMessage("Application will exit:" + ex.toString())
.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// throw it again
throw (RuntimeException) ex;
}
})
.show();
}
});
}

}

但我发现它有任何异常抛出,AlertDialog不会显示,相反,应用程序阻塞,一段时间后,它会显示一个系统对话框:

X app is not responding. Would you like to close it?
Wait | OK

我现在该怎么办?


更新

日志:

11-16 12:54:16.017: WARN/WindowManager(90): Attempted to add window with non-application token WindowToken{b38bb6a8 token=null}.  Aborting.

似乎错误来自 new AlertDialog.Builder(getApplicationContext());

但这是 Application 子类中的异常处理程序,我该如何为其设置 Activity 实例?

最佳答案

您不能从这里进行任何 UI 操作。只需开始另一个 Activity/启动屏幕。传递一个额外的 Intent 来表示崩溃并在该 Activity 中显示对话框。

    /*
* (non-Javadoc)
*
* @see
* java.lang.Thread.UncaughtExceptionHandler#uncaughtException(java.
* lang.Thread, java.lang.Throwable)
*/
@Override
public void uncaughtException(Thread t, final Throwable e) {
StackTraceElement[] arr = e.getStackTrace();
final StringBuffer report = new StringBuffer(e.toString());
final String lineSeperator = "-------------------------------\n\n";
report.append(DOUBLE_LINE_SEP);
report.append("--------- Stack trace ---------\n\n");
for (int i = 0; i < arr.length; i++) {
report.append( " ");
report.append(arr[i].toString());
report.append(SINGLE_LINE_SEP);
}
report.append(lineSeperator);
// If the exception was thrown in a background thread inside
// AsyncTask, then the actual exception can be found with getCause
report.append("--------- Cause ---------\n\n");
Throwable cause = e.getCause();
if (cause != null) {
report.append(cause.toString());
report.append(DOUBLE_LINE_SEP);
arr = cause.getStackTrace();
for (int i = 0; i < arr.length; i++) {
report.append(" ");
report.append(arr[i].toString());
report.append(SINGLE_LINE_SEP);
}
}
// Getting the Device brand,model and sdk verion details.
report.append(lineSeperator);
report.append("--------- Device ---------\n\n");
report.append("Brand: ");
report.append(Build.BRAND);
report.append(SINGLE_LINE_SEP);
report.append("Device: ");
report.append(Build.DEVICE);
report.append(SINGLE_LINE_SEP);
report.append("Model: ");
report.append(Build.MODEL);
report.append(SINGLE_LINE_SEP);
report.append("Id: ");
report.append(Build.ID);
report.append(SINGLE_LINE_SEP);
report.append("Product: ");
report.append(Build.PRODUCT);
report.append(SINGLE_LINE_SEP);
report.append(lineSeperator);
report.append("--------- Firmware ---------\n\n");
report.append("SDK: ");
report.append(Build.VERSION.SDK);
report.append(SINGLE_LINE_SEP);
report.append("Release: ");
report.append(Build.VERSION.RELEASE);
report.append(SINGLE_LINE_SEP);
report.append("Incremental: ");
report.append(Build.VERSION.INCREMENTAL);
report.append(SINGLE_LINE_SEP);
report.append(lineSeperator);

Log.e("Report ::", report.toString());
Intent crashedIntent = new Intent(BaseActivity.this, SplashActivity.class);
crashedIntent.putExtra(EXTRA_CRASHED_FLAG, "Unexpected Error occurred.");
crashedIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
crashedIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(crashedIntent);

System.exit(0);
// If you don't kill the VM here the app goes into limbo

}

另见:

Android UncaughtExceptionHandler that instantiates an AlertDialog breaks

Toast not showing up in UnCaughtExceptionHandler

How to start activity from UncaughtExceptionHandler if this is main thread crashed?

我是如何做到的:

我有一个扩展 Activity 的 BaseActivity,并在该 Activity 的 onCreate 中设置了 UncaughtExceptionHandler。我所有的 Activity 都扩展了 BaseActivity 而不是 Activity。

按键

  1. 您不能在Application.onCreate中设置异常处理程序,而是应该创建一个BaseActivity并将其设置在onCreate中方法。
  2. 启动SplashActivity后,我们应该调用System.exit(0)
  3. 我们不能将错误实例共享给SplashActivity,因为它会被销毁,相反,我们可以传递一些错误消息或将其保存在文件中。

关于android - 在 `Thread.setDefaultUncaughtExceptionHandler` 中显示一个对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13416879/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com