繁体   English   中英

奇怪的句柄必须是对话框中的有效异常

[英]Strange Handle must be valid Exception in Dialog

我收到报告说我的应用程序行为不端。 我显示同步成功对话框,让用户知道他们的数据已同步。 一些用户说他们没有得到同步成功对话框,进度条停留在 100%/100%,他们需要按返回退出。

这个问题不容易重现,事实上,我无法真正做到这一点,但我们创建的记录异常的日志显示了存在问题的区域。

using System;
using System.Collections.Generic;
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Classes;
using Microsoft.AppCenter.Crashes;
using Services;
using System.Threading;
using Helpers;

namespace Dialogs
{
    public class ProgressBarDialog : DialogFragment
    {
        private ProgressBar progressBar;
        private OnProgressFinished onProgressFinished;
        int _countSeconds;
        object _lock = new object();
        private TextView progressText;
        private LinearLayout titleBackground;
        private Button yes, no;
        private static string title, description;
        private OnItemClick callback;

        public interface OnItemClick
        {
            void OnItemClick(int id);
        }

        public interface OnProgressFinished
        {
            void OnProgressFinished(int id);
        }

        public static ProgressBarDialog NewInstance(Bundle bundle, string passedTitle, string passedDesc)
        {
            var fragment = new ProgressBarDialog();
            title = passedTitle;
            description = passedDesc;
            return fragment;
        }

        public override void OnAttach(Android.Content.Context context)
        {
            base.OnAttach(context);
            try
            {
                callback = (OnItemClick)context;
            }
            catch (Exception e)
            {
                Crashes.TrackError(e);
            }

            try
            {
                onProgressFinished = (OnProgressFinished)context;
            }
            catch (Exception e)
            {
                Crashes.TrackError(e);
            }
        }

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            //return customview for the fragment
            View view = inflater.Inflate(Resource.Layout.ProgressDialog, container, false);
            progressBar = view.FindViewById<ProgressBar>(Resource.Id.progressBar1);
            progressText = view.FindViewById<TextView>(Resource.Id.progressText);
            progressBar.Max = 100;
            SampleDatabase db = new SampleDatabase();
            SampleRESTFulService save = new SampleRESTFulService(Activity);
            List<Sample> samples = db.Get();
            int sampleCount = samples.Count;

            if (sampleCount > 0)
            {
                Activity.RunOnUiThread(async () =>
                {
                    using (var cancellationTokenSource = new CancellationTokenSource())
                    {
                        cancellationTokenSource.CancelAfter(TimeSpan.FromMinutes(5));

                        try
                        {
                            int successfullySynced = 0;
                            foreach (Sample sample in samples.ToArray())
                            {
                                cancellationTokenSource.Token.ThrowIfCancellationRequested();
                                if (sample.Tag.Equals("NR"))
                                {
                                    db.Delete(sample);
                                }

                                bool isSuccess = await save.SaveAsync(sample, cancellationTokenSource.Token);
                                if (isSuccess)
                                {
                                    ++successfullySynced;
                                    db.Delete(sample);
                                    samples.Remove(sample);
                                    progressBar.Progress = successfullySynced * 100 / sampleCount;
                                    progressText.Text = progressBar.Progress.ToString() + "%";
                                }
                                else if (isSuccess == false && cancellationTokenSource.IsCancellationRequested)
                                {
                                    onProgressFinished.OnProgressFinished(1);
                                    Dismiss();
                                }

                                CheckProgress(progressBar.Progress);
                            }
                        }
                        catch (Exception ex)
                        {
                            LoggerHelper.LogUser("ProgressBarDialog OnCreateView Error", ex.ToString());

                            Console.WriteLine(ex);
                        }

                        if (cancellationTokenSource.IsCancellationRequested)
                        {
                            Dismiss();
                            onProgressFinished.OnProgressFinished(1);
                        }
                        else if (progressBar != null && progressBar.Progress < 100)
                        {
                            Dismiss();
                            onProgressFinished.OnProgressFinished(1);
                        }
                    }
                });
            }
            return view;
        }

        // This is the function where the exception is kicking off.
        public void CheckProgress(int progress)
        {
            try
            {
                lock (_lock)
                {
                    if (progress >= 100)
                    {
                        Dismiss();
                        onProgressFinished.OnProgressFinished(0);
                    }
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.LogUser("ProgressBarDialog CheckProgress Error", ex.ToString());
            }

        }
    }
}

我得到的例外是:

2020 年 4 月 10 日下午 1:15:58] - System.ArgumentException:句柄必须有效。 Parameter name: instance at Java.Interop.JniEnvironment+InstanceMethods.CallObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00009] in <8cb56b2f65354c9f9fbb25da78a6cf09>:0 at Android.Runtime .JNIEnv.CallObjectMethod (System.IntPtr jobject, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x0000e] in:0 at Android.Content.ISharedPreferencesEditorInvoker.PutString (System.String key, System.String value) [0x0006c ] in:0 at Activities.SampleListActivity.OnProgressFinished (System.Int32 id) [0x00154] in:0 at Dialogs.ProgressBarDialog.CheckProgress (System.Int32 progress) [0x00022] in:0

我的回调 function 是这样的:

public void OnProgressFinished(int id)
        {
            if (id == 1)
            {
                if (!IsFinishing && informDialog != null && IsForeground)
                {
                    editor.PutString("Name", "");
                    editor.Commit();
                    updateAdapter();
                    informDialog.Dismiss();

                    SampleDatabase sDb = new SampleDatabase();
                    if(sDb.Get().ToArray().Count() > 0)
                    {
                        TrySyncAgain();
                    }
                    else
                    {
                        if (!IsFinishing && informDialog != null && IsForeground)
                        {
                            editor.PutString("PASSEDHERD", "");
                            editor.Commit();
                            AlertDialog.Builder alert = new AlertDialog.Builder(this);
                            alert.SetTitle("Sync Successful");
                            alert.SetMessage("You're samples have been synced successfully.");
                            alert.SetPositiveButton("Yes", (senderAlert, args) =>
                            {
                                LoggerHelper.LogUser(passedHerd.Tech_ID, string.Format("{0} clicked OK on sync successful.", passedHerd.Tech_ID));
                                LoggerHelper.Log(passedHerd.Herd_Test_ID, String.Format("User: {0}, pressed Yes on sync successful", passedHerd.Tech_ID));
                                End(true);
                            });
                            Dialog dialog = alert.Create();
                            dialog.Show();
                        }
                    }

                }
            }
            else if (id == 0)
            {
                if (!IsFinishing && informDialog != null && IsForeground)
                {
                    editor.PutString("PASSEDHERD", "");
                    editor.Commit();
                    AlertDialog.Builder alert = new AlertDialog.Builder(this);
                    alert.SetTitle("Sync Successful");
                    alert.SetMessage("You're samples have been synced successfully.");
                    alert.SetPositiveButton("Yes", (senderAlert, args) =>
                    {
                        LoggerHelper.LogUser(passedHerd.Tech_ID, string.Format("{0} clicked OK on sync successful.", passedHerd.Tech_ID));
                        LoggerHelper.Log(passedHerd.Herd_Test_ID, String.Format("User: {0}, pressed Yes on sync successful", passedHerd.Tech_ID));
                        End(true);
                    });
                    Dialog dialog = alert.Create();
                    dialog.Show();
                }
            }
        }

我尝试将 Null 传递给回调,但由于需要 Int 值,它不会接受它。

任何帮助将不胜感激。 谢谢

尝试使用 Firebase 测试实验室并安装 Crashlytics。 这将帮助您实时查看用户应用程序何时崩溃。 这对我帮助很大

该错误似乎与在 Xaramarin.Android 版本 7(“Cycle8”)中引入的 Tarjan 垃圾收集器有关。 但是,人们报告说旧的垃圾收集器可能会出现问题,但可能性较小。 查看您的代码:

else if (id == 0)
{
    if (!IsFinishing && informDialog != null && IsForeground)
    {
        editor.PutString("PASSEDHERD", ""); //-- This is the line causing you issues.
        editor.Commit();
        ...
     }
}

在我看来,在某些时候出现此错误的系统让垃圾收集器运行在错误的位置(可能是当系统在 memory 上运行不足时,但我无法对此进行测试)。

那里似乎有一些修复和解决方法。 第一个是将标志“ MONO_GC_PARAMS=bridge-implementation=old ”添加到构建中,但是,这只会降低问题的频率,而不是完全删除它。 接下来是使用 Xamarin.Android 7(“周期 7”)或更低版本不会出现问题,但是,如果您使用的是更新到最新版本,这是一个很大的降级。

最后,在 Xamarin.Essentials 的 0.9.0 版本中修补了一个修复程序,他们声称修复了 SharedPrefereces 问题。

这是我对此的研究:
https://xamarin.github.io/bugzilla-archives/51/51478/bug.html
https://xamarin.github.io/bugzilla-archives/47/47577/bug.html

https://github.com/xamarin/Essentials/issues/380
https://github.com/xamarin/Essentials/pull/386

暂无
暂无

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

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