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

相关问题