2016-11-22 177 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构建选项中禁用“使用共享运行时”。如果它没有帮助尝试 - 禁用“使用快速消极”。