簡體   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