【问题标题】:Xamarin:Forms: NullReferenceException after popping modal page (No MVVM)Xamarin:Forms:弹出模式页面后出现 NullReferenceException(无 MVVM)
【发布时间】:2021-02-25 04:36:38
【问题描述】:

我有一个带有 TabbedPage 布局的项目。其中一个 TabbedPages 有一个 ItemsSource 和一个 ItemSelected 事件处理程序,用于推送一个模式页面。当我弹出模式页面时,我收到 System.NullReferenceException: 'Object reference not set to an instance of an object。休息。我目前没有使用 MVVM,并且我在页面上我能想到的所有内容上都设置了 try/catch 块,但我找不到异常在哪里,但 Visual Studio 似乎表明异常不在我的代码中。调用栈:

0xFFFFFFFFFFFFFFFF in System.Diagnostics.Debugger.Mono_UnhandledException_internal  
0x1 in System.Diagnostics.Debugger.Mono_UnhandledException at /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Diagnostics/Debugger.cs:125,4  
0x20 in Android.Runtime.DynamicMethodNameCounter.1  
0x12 in System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw at /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:157,13  
0x6 in System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__7_0 at /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021,49  
0xC in Android.App.SyncContext.  
0xE in Java.Lang.Thread.RunnableImplementor.Run  
0x8 in Java.Lang.IRunnableInvoker.n_Run  
0x11 in Android.Runtime.DynamicMethodNameCounter.1

导致 NRE 的我的页面:

    {
        private ObservableCollection<Adventures> adventures;
        private List<Character> charactersList;
        string played = "No";
        string gmed = "No";
        public AdventuresPage()
        {
            InitializeComponent();
        }

        protected async override void OnAppearing()
        {
            try
            {
                var adventureList = await App.client.GetTable<Adventures>().Take(200).ToListAsync();
                adventures = new ObservableCollection<Adventures>(adventureList);

                AdventuresCollectionView.ItemsSource = adventures;
                AdventuresCollectionView.SelectedItem = null;
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine(ex.Message);
            }
            base.OnAppearing();
        }

        private async void AdventuresCollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var adventure = AdventuresCollectionView.SelectedItem as Adventures;
            string advName = (from a in adventures
                              where a.Id == adventure.Id
                              select a.AdventureName).First();
            await DetermineCredit(advName);

            if(adventure != null)
            {
                await Navigation.PushModalAsync(new AdventureDetailsPage(adventure, played, gmed));
            }
            else
            {
                AdventuresCollectionView.SelectedItem = null;
            }
        }

        private void AdvSearchBar_TextChanged(object sender, TextChangedEventArgs e)
        {
            var normalizedQuery = e.NewTextValue.ToString().ToLower() ?? "";
            AdventuresCollectionView.ItemsSource = adventures.Where(a => a.AdventureName.ToLowerInvariant().Contains(normalizedQuery)).ToList();
        }

        private async Task DetermineCredit(string name)
        {
            string advName = name;
            charactersList = new List<Character>(await App.client.GetTable<Character>().Where(a => a.AccountId == App.account.Id).ToListAsync());
            try
            {
                for (int c = 0; c < charactersList.Count(); c++)
                {
                    var chara = await App.client.GetTable<Character>().Where(ch => ch.Id == charactersList[c].Id).ToListAsync();
                    string charId = (from ch in chara
                                     select ch.Id).First().ToString();

                    var charAdv = await App.client.GetTable<CharAdventures>().Where(ca => ca.AdventureName == advName && ca.CharacterId == charId).ToListAsync();
                    string creditType = (from ch in charAdv
                                         where advName == ch.AdventureName
                                         select ch.CreditType).FirstOrDefault();
                    if (string.IsNullOrEmpty(creditType))
                    {
                        break;
                    }
                    else if (creditType == "Player" && played == "No")
                    {
                        played = "Yes";
                    }
                    else if (creditType == "GM" && gmed == "No")
                    {
                        gmed = "Yes";
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }

堆栈跟踪没有为我提供足够的信息来找出导致错误的原因,并且在我单步执行时直到覆盖的 OnAppearing() 方法完成后它才出现。我在搜索中没有看到任何与此类似的内容,我不明白为什么在第一次加载页面时不会出现这种情况,而只会在弹出模式页面时出现。任何建议将不胜感激。

【问题讨论】:

    标签: c# android xamarin.forms nullreferenceexception


    【解决方案1】:

    我多次执行负载,并注意到在关闭模式后,SelectionChanged 事件处理程序正在触发。

    注释掉两个AdventuresCollectionView.SelectedItem = null; 行允许页面在没有选择项目的情况下重新加载,并且没有 NullReferenceException。

    【讨论】:

      猜你喜欢
      • 2013-07-06
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多