Alright. I'm developing a Windows 8 (WinRT) app with C# and XAML, and I have a listbox which contains quite a few elements fetched from JSON. When you click one of the items, you'll be transferred to a new page showing the whole news item. This works.
However, whenever I click an item and I'm transferred, and I click the backbutton on the phone, the app crashes. I'm getting the following error, and I have no idea why it doesn't work! Any ideas?
private void NewsList_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
index = NewsList.SelectedIndex;
NewsItems newsContentGetSet = new NewsItems();
newsContentGetSet.news_id = newslistJson.ElementAt(index).news_id;
newsContentGetSet.news_title = newslistJson.ElementAt(index).news_title;
newsContentGetSet.news_abstract = newslistJson.ElementAt(index).news_abstract;
newsContentGetSet.news_content = newslistJson.ElementAt(index).news_content;
newsContentGetSet.news_author = newslistJson.ElementAt(index).news_author;
newsContentGetSet.news_date_formatted = newslistJson.ElementAt(index).news_date_formatted;
newsContentGetSet.user_firstname = newslistJson.ElementAt(index).user_firstname;
newsContentGetSet.user_lastname = newslistJson.ElementAt(index).user_lastname;
App.newsContentGetSet = newsContentGetSet;
NavigationService.Navigate(new Uri("/NewsPage.xaml?language=" + chosenLanguage, UriKind.Relative));
}
Exception:
System.ArgumentOutOfRangeException was unhandled Message= Parameter name: index StackTrace: at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) at System.ThrowHelper.ThrowArgumentOutOfRangeException() at System.Collections.Generic.List
1.get_Item(Int32 index) at System.Linq.Enumerable.ElementAt[TSource](IEnumerable
1 source, Int32 index) at MunchApp3._0.MainPage.NewsList_SelectionChanged_1(Object sender, SelectionChangedEventArgs e) at System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArgs e) at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List1 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.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.DependencyObject.SetValueInternal(DependencyProperty dp, Object value) at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) at System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value) at MunchApp3._0.MainPage.webClientNews_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e) at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e) at System.Net.WebClient.DownloadStringOperationCompleted(Object arg) at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark) at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at System.Delegate.DynamicInvokeOne(Object[] args) at System.MulticastDelegate.DynamicInvokeImpl(Object[] args) at System.Delegate.DynamicInvoke(Object[] args) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority) at System.Windows.Threading.Dispatcher.OnInvoke(Object context) at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args) at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args) at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
NewsList.SelectedIndex
when you enter the method? – Jim Mischelnewslist.Json
collection? Have you checked theCount
property (or return value of theCount()
method)? – Jim MischelSelectedIndex
is 0, andnewslist.Json
contains 2 items? That's ... unusual. How about putting a test in the code that says,if (NewsList.SelectedIndex < 0 || NewsList.SelectedIndex >= newslist.Json.Count) { throw new Exception(); }
... put a breakpoint on thethrow
and see what happens when you run your program. – Jim Mischel