Android上未处理的例外情况

时间:2017-09-22 12:20:48

标签: c# android xamarin

我有这段代码:

    protected override void OnCreate(Bundle bundle)
    {
        AndroidEnvironment.UnhandledExceptionRaiser += HandleAndroidException;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());

    }

    private void HandleAndroidException(object sender, RaiseThrowableEventArgs e)
    {
        StreamWriter sw = File.CreateText(Android.App.Application.Context.GetExternalFilesDir(null).Path + "logerror.txt");
        sw.WriteLine(e.Exception.ToString());
        sw.Close();           
    }

它正在工作,但是我想在HandleAndroidException中显示带有异常信息的AlertDialog,所以我写了这段代码:

 private void HandleAndroidException(object sender, RaiseThrowableEventArgs e)
    {
        AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
        builder1.SetMessage(e.ToString());
        builder1.SetCancelable(true);
        AlertDialog alert11 = builder1.Create();
        alert11.Show();
    }

而且......它不起作用。我认为当应用程序抛出异常时,不允许使用AlertDialog - 这是真的吗?当应用程序抛出未处理的异常时,有没有办法通过应用程序(在DialogBox中)显示异常信息?

1 个答案:

答案 0 :(得分:1)

  

我想在HandleAndroidException中显示带有异常信息的AlertDialog

您无法使用AlertDialog显示错误消息。这是另一种选择,您可以显示Activity作为显示错误消息的对话框。

我写了一个关于如何实现此功能的简单演示,效果如this。您可以在此GitHub Repository中看到它。这是我的代码:

抓住例外:

protected override void OnCreate(Bundle bundle)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    AndroidEnvironment.UnhandledExceptionRaiser += HandleAndroidException;

    base.OnCreate(bundle);
}

打开一个新的Activity以显示错误消息:

private void HandleAndroidException(object sender, RaiseThrowableEventArgs e)
{
    Intent intent = new Intent(this, typeof(CrashDialog));
    intent.PutExtra("Error_Text", e.ToString());
    intent.SetFlags(ActivityFlags.NewTask);
    this.StartActivity(intent);
    Java.Lang.JavaSystem.Exit(0);// Close this app process
}

将此CrashDialog活动用作对话框:

[Activity(
    Label = "CrashDialog", 
    LaunchMode = LaunchMode.SingleTask,
    Theme = "@style/alert_dialog", 
    ScreenOrientation = ScreenOrientation.Portrait,
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.SmallestScreenSize | ConfigChanges.Keyboard| ConfigChanges.KeyboardHidden|ConfigChanges.Navigation),
    ]
public class CrashDialog : Activity
{
    private Button btnExit, btnRestart;
    public string errorMessage;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_crash);
        errorMessage = Intent.GetStringExtra("Error_Text");
        this.SetFinishOnTouchOutside(false);
        InitView();
    }

    private void InitView()
    {
        btnExit = (Button)FindViewById(Resource.Id.cash_exit);
        btnExit.Click += (sender, e) =>
        {
            Exit();
        };
        btnRestart = (Button)FindViewById(Resource.Id.cash_restart);
        btnRestart.Click += (sender, e) =>
        {
            Restart();
        };
        TextView errorText = FindViewById<TextView>(Resource.Id.content);
        errorText.Text = errorMessage;
    }

    public override void OnBackPressed()
    {
        base.OnBackPressed();
        Exit();
    }

    private void Exit()
    {
        Java.Lang.JavaSystem.Exit(0);
        Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
    }

    private void Restart()
    {
        Intent intent = BaseContext.PackageManager.GetLaunchIntentForPackage(BaseContext.PackageName);
        intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
        StartActivity(intent);
        Exit();
    }
}

它的风格:

<style name="alert_dialog" parent="android:Theme.Dialog">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">false</item>
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:windowAnimationStyle">@null</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.4</item>
</style>