如何调试崩溃应用程序

时间:2016-11-22 11:23:47

标签: android debugging xamarin

我正在使用Xamarin(在Visual Studio 2015中)处理Android应用程序。

当我在调试模式下运行我的应用程序时,某个时候应用程序停止并显示消息" MyApplication已停止"。

我已在MainActivity中添加了此代码:

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

我在两个函数中添加了断点添加日志,但是我在日志中看不到任何跟踪,并且没有到达断点。

如何调试此类问题?

2 个答案:

答案 0 :(得分:1)

试一试:在Android构建选项中禁用“使用共享运行时”。如果没有帮助尝试 - 禁用“使用快速取消”。

答案 1 :(得分: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