没有白色屏幕的Android Uncaught
我想避免应用程序崩溃,然后我开发了uncaughtException方法来调用另一个活动;它很好用。如果应用程序崩溃了任何活动,将调用uncaughtException,然后显示MyErrorActivity。 问题是当我试图在崩溃后关闭应用程序时。当我按下后退按钮时,ErrorActivity关闭,但随后出现一个带有actionbar的白色屏幕,几秒钟后,ErrorActivity再次被调用。因此,除非在taskmanager中完成应用程序,否则无法关闭该应用程序没有白色屏幕的Android Uncaught,android,android-activity,crash,uncaught-exception,Android,Android Activity,Crash,Uncaught Exception,我想避免应用程序崩溃,然后我开发了uncaughtException方法来调用另一个活动;它很好用。如果应用程序崩溃了任何活动,将调用uncaughtException,然后显示MyErrorActivity。 问题是当我试图在崩溃后关闭应用程序时。当我按下后退按钮时,ErrorActivity关闭,但随后出现一个带有actionbar的白色屏幕,几秒钟后,ErrorActivity再次被调用。因此,除非在taskmanager中完成应用程序,否则无法关闭该应用程序 @Override publ
@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();
}
}
当你在应用程序崩溃时执行
system.exit()
时,这意味着它不是一个常规的system.exit()
,但它是一个例外的,这意味着你最好使用system.exit(1)
,而不是system.exit(0)
,这可能会解决你的问题。这来自“终止状态。按照惯例,非零状态代码表示异常终止。”
更新:
尝试将意图标志从NEW\u TASK
更改为CLEAR\u TOP
:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
我找到了解决办法。只需添加一个
意图。标记活动\u清除任务
,如下所示:
@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
}
那么你想在按下按钮时关闭应用程序?为什么不在ErrorActivity中实现@Override onBackPressed并执行类似new Finalizer()的操作。killApp(true);有吗?再想想,试试moveTaskToBack(true)而不是System.exit()或new Finalizer().killApp(true)。我试过moveTaskToBack(true),但当我再次尝试打开应用时,设备停止工作:(我已经试过了,但没有工作。我也试过System.exit(2)、System.exit(10)等等。