0

我一直在試圖實現一個自定義UncaughtExceptionHandler到我的應用程序,我發現了圖書館Ereza/CustomActivityOnCrash。Ereza CustomActivityOnCrash startActivity與意圖

我已經添加了自述文件(https://github.com/Ereza/CustomActivityOnCrash)中描述的所有代碼,但我似乎無法使其工作。當我強制我的應用程序崩潰時,會彈出一個白色屏幕,然後立即消失。

任何想法可能導致這種情況?下面是我的一些代碼

AndroidManifest

<activity 
     android:name="com.test.SplashActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="com.test.MainActivity" 
     android:label="MainActivity" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme" 
     android:windowSoftInputMode="adjustPan"> 
     <intent-filter> 
      <action android:name="cat.ereza.customactivityoncrash.RESTART" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name="com.test.ErrorActivity" 
     android:label="Error" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme" 
     android:windowSoftInputMode="adjustPan"> 
     <intent-filter> 
      <action android:name="cat.ereza.customactivityoncrash.ERROR" /> 
     </intent-filter> 
    </activity> 

應用類

public class TestApplication extends Application { 

    private static TestApplication mInstance; 
    private TimeZone usersDefaultTimeZone; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mInstance = this; 

     //Customization for CustomActivityOnCrash 
     //Show (TRUE) or Hide (FALSE) the stack trace on the error activity 
     CustomActivityOnCrash.setShowErrorDetails(false); 

     //Enable Restart (TRUE) 'Restart App' button 
     //Disable Restart (FALSE) 'Close App' button 
     CustomActivityOnCrash.setEnableAppRestart(true); 

     //This sets a custom error activity class instead of the default one. 
     CustomActivityOnCrash.setErrorActivityClass(ErrorActivity.class); 

     //This sets a EventListener to be notified of events regarding the error activity, 
     //CustomActivityOnCrash.setEventListener(new CustomEventListener()); 

     //Specify the Activity to to restart the application 
     CustomActivityOnCrash.setRestartActivityClass(MainActivity.class); 

     //Install CustomActivityOnCrash 
     CustomActivityOnCrash.install(this); 

     //Now initialize your error handlers as normal 
     Fabric.with(this, new Crashlytics()); 
    } 

    public static synchronized TestApplication getInstance() { 
     return mInstance; 
    } 

    static class CustomEventListener implements CustomActivityOnCrash.EventListener{ 

     @Override 
     public void onLaunchErrorActivity() { 
      Log.i("BookingApp", "onLaunchErrorActivity()"); 
      Crashlytics.logException(new Exception("Unknown Exception Caught & Error screen displayed to user")); 
     } 

     @Override 
     public void onRestartAppFromErrorActivity() { 
      Log.i("BookingApp", "onRestartAppFromErrorActivity()"); 
      Answers.getInstance().logCustom(new CustomEvent("Restart After Crash")); 
     } 

     @Override 
     public void onCloseAppFromErrorActivity() { 
      Log.i("BookingApp", "onCloseAppFromErrorActivity()"); 
      Answers.getInstance().logCustom(new CustomEvent("Close After Crash")); 
     } 
    } 
} 
+0

請提供logcat的 –

回答

1

喜把下面的應用類的代碼和修改,只要你想爲HANDELING未處理的異常:

@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 e) { 

      e.printStackTrace(); // not all Android versions will print the stack trace automatically 


//Work what you want to do.... 

//start new activity with clearing task 
Intent intent = new Intent(this, ActivityClass.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     startActivity(intent); 

//end 
      System.exit(2); // kill off the crashed app 
     } 

希望這個幫助,我用在我的應用程序...作品。

+0

嗨拉梅什,是啊,這是一個很好的解決方案,但我想錯誤記錄到Crashlytics並重新啓動應用程序 – RheeBee

+0

是的,我更新了明確的前一個任務開始活動了答案。您可以接受這個答案.. –

0

經過一番調查後,我發現這個庫/代碼不喜歡在調試模式下運行。如果我在我的設備上安裝應用程序並從那裏啓動應用程序(而不是使用Android Studio中的Debug),它似乎工作正常。

0

這很可能是由於ErrorActivity未被指定爲另一個進程的一部分而引起的。

從庫文件,關於自定義錯誤行爲的用法:

如果您使用此,活動必須在你AndroidManifest.xml宣佈,與process設置爲:error_activity

相關問題