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

问题描述 投票:0回答:1

我收到有关我的应用程序行为异常的报告。我显示了一个同步成功对话框,让用户知道他们的数据已同步。一些用户曾说他们未获得同步成功对话框,并且进度栏仅停留在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());
            }

        }
    }
}

我得到的例外是:

4/10/2020 1:15:58 PM]-System.ArgumentException:句柄必须有效。参数名称:实例在<8cb56b2f65354c9f9f9fbb25da78a6cf09>:0在Android.Runtime.JNIEnv.CallObjectMethod(System.IntPtr jobject,System.IntPtr jmethod,Android.Runtime.JValue * parms)中的[0x0000e],在:0中在Android.Content.ISharedPreferencesEditorInvoker.PutString(System.String键,System.String值)中的[0x0006c] :: 0在:0中的Activities.SampleListActivity.OnProgressFinished(System.Int32 id)[0x00154]中在Dialogs.ProgressBarDialog.CheckProgress(System.Int32进度)中的[0x00022]:0中,>

我的回调函数如下:

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值,因此无法接收。

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

我收到有关我的应用程序行为异常的报告。我显示了一个同步成功对话框,让用户知道他们的数据已同步。一些用户说他们没有获得同步...

c# android exception xamarin.android
1个回答
0
投票

尝试使用Firebase Testlab并安装Crashlytics。当用户应用崩溃时,这将帮助您实时查看。这对我有很大帮助

© www.soinside.com 2019 - 2024. All rights reserved.