Android 未捕获且无白屏

标签 android android-activity crash uncaught-exception

我想避免应用程序崩溃,然后我开发了 uncaughtException 方法来调用另一个 Activity ;效果很好。如果应用程序崩溃了任何 Activity ,则会调用 uncaughtException,然后显示我的 ErrorActivity。 问题是当我尝试在崩溃后关闭应用程序时。当我按下后退按钮时,ErrorActivity 将关闭,但随后会显示带有操作栏的白屏,几秒钟后,ErrorActivity 会再次被调用。因此,除非在任务管理器中完成应用程序,否则无法关闭应用程序。

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

    Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
    {
        @Override
        public void uncaughtException (Thread thread, Throwable e)
        {
            handleUncaughtException (thread, e);
        }
    });

}

public void handleUncaughtException (Thread thread, Throwable exception)
{

    StringWriter _stackTrace = new StringWriter();
    exception.printStackTrace(new PrintWriter(_stackTrace));

    Intent _intent = new Intent();
    _intent.setAction ("com.mypackage.ERROR_ACTIVITY");
    _intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
    _intent.putExtra("error", _stackTrace.toString());
    startActivity(_intent);

    System.exit(0);

}

最佳答案

试试这个:

public void handleUncaughtException (Thread thread, Throwable exception)
{

    StringWriter _stackTrace = new StringWriter();
    exception.printStackTrace(new PrintWriter(_stackTrace));

    Intent _intent = new Intent();
    _intent.setAction ("com.mypackage.ERROR_ACTIVITY");
    _intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
    _intent.putExtra("error", _stackTrace.toString());
    startActivity(_intent);

    try {
        runOnUiThread(new Runnable() {

        @Override
        public void run() {
            finish();
        }
        });
            Thread.sleep(300);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }    
}

关于Android 未捕获且无白屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27525713/

相关文章:

android - 结合使用ProgressDialog和线程时,应用程序因方向更改而崩溃

c++ - 数据竞赛?段错误,但是问题出在哪里呢?

android - 防止 SQLite 数据库中的数据重复

android - 在android中将值(value)从一个 Activity 传递到另一个 Activity

Android - 后台 Activity 会发生什么?

android - 在 Android 上如何将对象从一个 Activity 传递到另一个 Activity?

Java - 应用程序在 Thread.sleep 方法上崩溃

java - 来自异步类的多个嵌套回调

java - 通过签名比较 SSL 证书 : is it enough?

android - GridLayout 不显示