繁体   English   中英

Android上未处理的异常

[英]Unhandled exceptions on Android

我有以下代码:

    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中)显示有关异常的信息?

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

您不能使用AlertDialog显示错误消息。 这是一个替代选择,您可以将“ Activity显示为对话框以显示错误消息。

我写了有关如何实现这个功能,就像影响一个简单的演示这个 您可以在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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM