返回导航时出现NullReferenceException
本文关键字:NullReferenceException 导航 返回 | 更新日期: 2023-09-27 18:23:53
我正在开发Windows Phone 8+应用程序。我有一个页面,其中包含项目的详细信息。每个Item都可以是另一个Item的一个组件,因此我有两个ListBox,其中包含可以组成或组成当前Item的其他Item。
JItem.Item currItem;
List<JItem.Item> components;
List<JItem.Item> isComponentOf;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
currItemId = NavigationContext.QueryString["item_id"];
currItem = (JItem.Item)App.itemsDict[currItemId];
LayoutRoot.DataContext = currItem;
components = new List<JItem.Item>();
isComponentOf = new List<JItem.Item>();
foreach (var x in currItem.components)
{
components.Add((JItem.Item)App.itemsDict[x]);
}
foreach (var x in currItem.composes)
{
isComponentOf.Add((JItem.Item)App.itemsDict[x]);
}
componentsLB.ItemsSource = components;
composesLB.ItemsSource = isComponentOf;
if (components.Count == 0)
componentsLabel.Visibility=Visibility.Collapsed;
if (isComponentOf.Count == 0)
isComponentLabel.Visibility=Visibility.Collapsed;
}
如果我在componentsLB
列表框或isComponentOf
列表框中向前导航选择一个项目,一切都很好。如果我通过后退按钮返回到某个页面,即使components
包含项目并且分配了componentsLB
,我也会在componentsLB.ItemsSource = components;
行得到一个System.NullReferenceException
。
由于它在NavigationStack上,所以页面不会被重新初始化(构造函数中的InitializeComponent()
没有被调用),而是分配了ListBox。
当通过Back导航访问页面时,我可以使用一些变通方法(例如不重新加载数据),但我想了解是什么导致了错误。
这是出现气泡的异常的Stacktrace:
at DOTA2_Pocket.ItemDetails.componentsLB_SelectionChanged(Object sender, SelectionChangedEventArgs e)
at System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArgs e)
at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedItems, List`1 selectedItems)
at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
at System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e)
at System.Windows.Controls.ListBox.OnItemsChanged(NotifyCollectionChangedEventArgs e)
at System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
at System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
at System.Windows.Controls.ItemCollection.UpdateItemsSourceList(IEnumerable newItemsSource)
at System.Windows.Controls.ItemsControl.ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
at System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value)
at DOTA2_Pocket.ItemDetails.OnNavigatedTo(NavigationEventArgs e)
at Microsoft.Phone.Controls.PhoneApplicationPage.InternalOnNavigatedTo(NavigationEventArgs e)
at Microsoft.Phone.Controls.PhoneApplicationPage.Microsoft.Phone.Controls.IPhoneApplicationPage.InternalOnNavigatedToX(NavigationEventArgs e)
at System.Windows.Navigation.NavigationService.RaiseNavigated(Object content, Uri uri, NavigationMode mode, Boolean isNavigationInitiator, IPhoneApplicationPage existingContentPage, IPhoneApplicationPage newContentPage)
at System.Windows.Navigation.NavigationService.CompleteNavigation(DependencyObject content, NavigationMode mode)
at System.Windows.Navigation.NavigationService.<>c__DisplayClass7.<NavigateCore_StartNavigation>b__4()
根据您提供的信息,它看起来像是您的App.itemsDict包含一个空引用,您正在将其添加到组件列表变量中。ListBox的SelectedChanged事件随后引发NullReference异常,因为它正试图对该null引用执行操作。