【问题标题】:Android Uncaught with no white screenAndroid Uncaught 没有白屏
【发布时间】:2014-12-17 12:31:30
【问题描述】:

我想避免应用程序崩溃,然后我开发了 uncaughtException 方法来调用另一个活动;它工作正常。如果应用程序崩溃了任何活动,则会调用 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);

}

【问题讨论】:

  • 所以你想关闭应用程序 onBackPressed?为什么不直接在 ErrorActivity 中实现 @Override onBackPressed 并执行类似 new Finalizer().killApp(true);那里?
  • 再想一想,试试 moveTaskToBack(true) 而不是 System.exit() 或 new Finalizer().killApp(true)。
  • 我试过 moveTaskToBack(true),但是当我再次尝试打开应用程序时,设备停止工作:(

标签: android android-activity crash uncaught-exception


【解决方案1】:

试试这个:

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();
    }    
}

【讨论】:

    【解决方案2】:

    当您在应用程序崩溃时执行system.exit() 时,这意味着它不是常规的system.exit(),而是一个异常,这意味着您最好使用system.exit(1) 而不是@ 987654325@ 这可能会解决您的问题。这是来自Java Documentation终止状态。按照惯例,非零状态码表示异常终止。”。

    更新:

    尝试将意图标志从 NEW_TASK 更改为 CLEAR_TOP

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    

    【讨论】:

    • 我已经尝试过了,但效果不佳。我也试过system.exit(2)、system.exit(10)等等。
    【解决方案3】:

    我找到了解决方案。只需添加一个Intent.FLAG_ACTIVITY_CLEAR_TASK,如下所示:

    @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("br.com.magazineluiza.mobilevendas.ERROR_ACTIVITY");
    
        _intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // Here is the solution
    
        _intent.putExtra("error", _stackTrace.toString());
        startActivity(_intent);
    
        System.exit(0); // or System.exit(1) It doesn't matter
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多