gpt4 book ai didi

Android 未捕获且无白屏

转载 作者:行者123 更新时间:2023-12-01 23:08:38 26 4
gpt4 key购买 nike

我想避免应用程序崩溃,然后我开发了 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/

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