2016-11-22 229 views
0

我正在爲使用Xamarin(適用於Visual Studio 2015)的Android應用程序工作。如何調試崩潰應用程序

當我在調試模式下運行我的應用程序時,有時應用程序停止並顯示消息「MyApplication has stopped」。

我在MainActivity中添加以下代碼:

// Catch Exception 
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; 
AndroidEnvironment.UnhandledExceptionRaiser += AndroidEnvironment_UnhandledExceptionRaiser; 

我加斷點兩種功能添加日誌,但我看不出有什麼痕跡,日誌和斷點沒有達到。

如何調試此類問題?

回答

1

您需要通過持久記錄器調試您的應用程序,即adb logcat。令人遺憾的是,註冊一個未處理的異常處理程序並不能保證它將成爲一個「全面通用」的程序,因爲它可能永遠達不到這一點。因此,對於這些類型的問題,您需要結合使用adb logcatConsole.WriteLine。當你想看看這個處理程序中發生了什麼時,請考慮以下注意事項,但也可以使用adb logcat來首先查看崩潰的原因。

/// <summary> 
/// When app-wide unhandled exceptions are hit, this will handle them. Be aware however, that typically 
/// android will be destroying the process, so there's not a lot you can do on the android side of things, 
/// but your xamarin code should still be able to work. so if you have a custom err logging manager or 
/// something, you can call that here. You _won't_ be able to call Android.Util.Log, because Dalvik 
/// will destroy the java side of the process. 
/// </summary> 

protected void HandleUnhandledException (object sender, UnhandledExceptionEventArgs args) 
{ 
    Exception e = (Exception) args.ExceptionObject; 

    // log won't be available, because dalvik is destroying the process 
    //Log.Debug (logTag, "MyHandler caught : " + e.Message); 
    // instead, your err handling code shoudl be run: 
    Console.WriteLine ("========= MyHandler caught : " + e.Message); 
} 

https://github.com/xamarin/monodroid-samples/blob/fb9d4ed266bdf68bb1f9fa9933130b285712ec82/AdvancedAppLifecycleDemos/HandlingCrashes/App.cs

1

嘗試一下:在Android構建選項中禁用「使用共享運行時」。如果它沒有幫助嘗試 - 禁用「使用快速消極」。