0

アプリがクラッシュして動作するアクティビティを開始するのを防ぐために、ExceptionHandler を実装しました。私の問題は、エラーダイアログが表示されなくなったことです。ExceptionHandler のコードは次のとおりです。

public class MyExceptionHandler implements
    java.lang.Thread.UncaughtExceptionHandler {
private final Context myContext;
private final Class<?> myActivityClass;

public MyExceptionHandler(Context context, Class<?> c) {

    myContext = context;
    myActivityClass = c;
}

public void uncaughtException(Thread thread, Throwable exception) {

    StringWriter stackTrace = new StringWriter();
    exception.printStackTrace(new PrintWriter(stackTrace));
    System.err.println(stackTrace);// You can use LogCat too
    Intent intent = new Intent(myContext, myActivityClass);
    String s = stackTrace.toString();
    //you can use this String to know what caused the exception and in which Activity
    intent.putExtra("uncaughtException",
            "Exception is: " + stackTrace.toString());
    intent.putExtra("stacktrace", s);
    myContext.startActivity(intent);
    //for restarting the Activity
    Process.killProcess(Process.myPid());
    System.exit(0);
}
}

アプリケーションを再起動する前に、通常の黒いクラッシュダイアログを表示する方法を知っている人はいますか?

前もって感謝します!

4

1 に答える 1

0

私は解決策を見つけました。問題は次のとおりです。

 Intent intent = new Intent(myContext, myActivityClass);
String s = stackTrace.toString();
//you can use this String to know what caused the exception and in which Activity
intent.putExtra("uncaughtException",
        "Exception is: " + stackTrace.toString());
intent.putExtra("stacktrace", s);
myContext.startActivity(intent);

startActivity を削除して、ダイアログの問題を解決し、アプリケーションを期待どおりに再起動しました。

編集:

ダイアログプロンプトは、データが欠落しているために2番目のクラッシュが発生したためだけなので、この答えは正しくありません...

于 2015-09-08T19:30:14.417 に答える