崩溃后打开AlertDialog

时间:2015-01-26 14:26:17

标签: android exception-handling alertdialog crash-reports

我制作了一个抽象片段,其他片段扩展如下:

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

    final Thread.UncaughtExceptionHandler oldHandler = Thread.getDefaultUncaughtExceptionHandler();

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
            Writer writer = new StringWriter();
            PrintWriter printWriter = new PrintWriter(writer);
            paramThrowable.printStackTrace(printWriter);
            String s = writer.toString();
            Utils.logToFile(s);
            emailReport(s); ///<<<<<

            if (oldHandler != null) oldHandler.uncaughtException(paramThread, paramThrowable); //Delegates to Android's error handling
            else System.exit(2); //Prevents the service/app from freezing
        }
    });

}

在将流程交回操作系统之前(将异常存储在文件中之后),我致电emailReport(s);

private void emailReport(final String report) {
        new AlertDialog.Builder(getActivity())
                .setTitle(getString(R.string.crash_report))
                .setMessage(getString(R.string.send_crash_report))
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_SEND);
                        intent.setType("message/rfc822");
                        intent.putExtra(Intent.EXTRA_SUBJECT, "Crash Report");
                        intent.putExtra(Intent.EXTRA_TEXT, report);
                        Intent mailer = Intent.createChooser(intent, null);
                        startActivity(mailer);
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }

我希望在操作系统获得控制权之前显示警报对话框,以便用户可以打开他的电子邮件客户端向我发送崩溃或取消并关闭应用程序。调试在创建警告对话框的行上停止,但之后,它会跳转到此方法的末尾,然后从那里返回到旧处理程序。

这可能是另一个例外,但我不知道这是肯定的,我无法抓住它,因为我已经在处理当前异常的中间。代码中有什么问题吗?如果没有,我怎样才能看到尝试创建警报对话框时会发生什么?

更新:我将处理行放在对话框的setNegativeButton中,认为它已经显示但是立即关闭但没有帮助:

        if (oldHandler != null) oldHandler.uncaughtException(paramThread, paramThrowable); //Delegates to Android's error handling
        else System.exit(2); //Prevents the service/app from freezing
    }

1 个答案:

答案 0 :(得分:0)

我想问题是你将imediatly称为内部调用exit的默认处理程序,因此你的对话框从未显示过。所以你的方法应该是这样的:

private void emailReport(final String report,
                         final Thread.UncaughtExceptionHandler oldHandler) {
    new AlertDialog.Builder(getActivity())
            .setTitle(getString(R.string.crash_report))
            .setMessage(getString(R.string.send_crash_report))
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(Intent.ACTION_SENDTO);
                    intent.setData(Uri.parse("mailto:support@example.com"));
                    intent.setType("message/rfc822");
                    intent.putExtra(Intent.EXTRA_SUBJECT, "Crash Report");
                    intent.putExtra(Intent.EXTRA_TEXT, report);
                    Intent mailer = Intent.createChooser(intent, null);
                    startActivity(mailer);
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    if(oldHandler != null) {
                        //Delegates to Android's error handling
                        oldHandler.uncaughtException(paramThread, paramThrowable);
                    } else {
                         // Force to close the app now after handling the crash bug
                         System.exit(2);
                    }
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
}

不要错过删除最后两行:

if (oldHandler != null) oldHandler.uncaughtException(paramThread, paramThrowable); //Delegates to Android's error handling
else System.exit(2); //Prevents the service/app from freezing