c# - 单体机器人 : Unhandled Exception Recovery

标签 c# android exception-handling mono xamarin.android

我正在尝试向我的应用程序添加一个默认处理程序,以便我可以从其他未处理的异常中恢复。

我发现了 Android/MonoDroid 提供的三种机制,据我所知,应该可以实现这一点,但我无法让其中任何一种发挥作用。这是我的代码:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace TestApp {
    [Android.App.Activity(Label = "TestApp", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity {
        protected override void OnCreate(Bundle bundle) {
            base.OnCreate(bundle);
            SetContentView(new LinearLayout(this));

            //set up handlers for uncaught exceptions:
            //Java solution
            Java.Lang.Thread.DefaultUncaughtExceptionHandler = new ExceptionHandler(this);
            //MonoDroid solution #1
            AndroidEnvironment.UnhandledExceptionRaiser += AndroidEnvironment_UnhandledExceptionRaiser;
            //MonoDroid solution #2
            AppDomain.CurrentDomain.UnhandledException += delegate { new Alert(this, "AppDomain.CurrentDomain.UnhandledException", "error"); };

            //throw an exception to test
            throw new Exception("uncaught exception");
        }

        void AndroidEnvironment_UnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e)
        {
            //found a suggestion to set the Handled flag=true, but it has no effect
            new Android.Runtime.RaiseThrowableEventArgs(e.Exception).Handled = true;
            new Alert(this, "AndroidEnvironment.UnhandledExceptionRaiser", "error");
        }
    }

    public class ExceptionHandler : Java.Lang.Object, Java.Lang.Thread.IUncaughtExceptionHandler {
        private Context _context;
        public ExceptionHandler(Context c) { _context = c; }
        public void UncaughtException(Java.Lang.Thread thread, Java.Lang.Throwable ex) {
            new Alert(_context, "java exception handler");
        }
    }

    public class Alert {
        public Alert(Context c, string src, string title = "alert") {
            Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(c);
            builder.SetTitle(title);
            builder.SetMessage(src);
            builder.SetPositiveButton("Ok", delegate { });
            Android.App.AlertDialog alert = builder.Create();
            alert.Show();
        }
    }
}

任何帮助将不胜感激,谢谢!

最佳答案

您可以在 AppDomain.CurrentDomain.UnhandledException 事件中捕获大部分异常。但是你不能在这个委托(delegate)中调用 AlertDialog,因为: 1. 应用程序崩溃。 2. 至少调用UnhandledException 事件。 3. alert.Show() 异步执行(在 UI 线程中)

因此,您应该使用同步操作(例如 System.IO)。您不应使用 native UI 操作,因为它们是异步的。

关于c# - 单体机器人 : Unhandled Exception Recovery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15978070/

相关文章:

c# - 如何在 .NET 3 控制台应用程序的打印行中间获取输入

c# - 想安装 DNN 7 但出现 Configuration Error 错误

安卓模拟器 : both soft input and hard keyboard input

android - 如何在Android中使用opencv自动裁剪从相机拍摄的名片图像

c# - 使用正则表达式获取无前缀/无转义的文本

c# - Microsoft Outlook 添加超链接到电子邮件 C#

android - 如何在 Android 上创建透明 Activity?

c# - ASP.NET (C#) Web 服务中的异常处理

c++ - C++ 中的全局异常处理

javascript - 如何在 Internet Explorer 中获取 javascript 堆栈跟踪。 e.stack 返回 "Undefined"