我想避免应用程序崩溃,然后开发了uncaughtException方法来调用另一个 Activity 。它工作正常。如果应用程序使任何 Activity 崩溃,则将调用uncaughtException,然后显示我的ErrorActivity。
问题是当我崩溃后尝试关闭应用程序时。当我按下后退按钮时,ErrorActivity关闭,但是随后出现带操作栏的白屏,几秒钟后再次调用ErrorActivity。因此,除非在taskmanager中完成应用程序的确定,否则无法将其关闭。

@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 - 没有白屏的Android,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27525713/

10-10 11:54