Bug 35546 - Android Scrolling a ListView crashes app on tablet
Summary: Android Scrolling a ListView crashes app on tablet
Status: RESOLVED DUPLICATE of bug 51704
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 1.5.1
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-11-04 14:50 UTC by Caan Mee
Modified: 2017-11-09 14:19 UTC (History)
10 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED DUPLICATE of bug 51704

Description Caan Mee 2015-11-04 14:50:24 UTC
Using a Samsung Galaxy Note 10.1 with Android 4.4 I get the following exception, when Scrolling a Xamarin.Forms ListView

System.ArgumentOutOfRangeExceptionIndex was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Raw
at System.ThrowHelper.ThrowArgumentOutOfRangeException () <0x00030>
at System.Collections.Generic.List`1<object>.get_Item (int) <0x00027>
at Cadenza.Collections.OrderedDictionary`2<object, Xamarin.Forms.TemplatedItemsList`2<Xamarin.Forms.ItemsView`1<Xamarin.Forms.Cell>, Xamarin.Forms.Cell>>.get_Item (int) <0x0004b>
at Xamarin.Forms.TemplatedItemsList`2<Xamarin.Forms.ItemsView`1<Xamarin.Forms.Cell>, Xamarin.Forms.Cell>.GetGroup (int) <0x0003f>
at Xamarin.Forms.Platform.Android.ListViewAdapter.get_Count () <0x0006b>
at Android.Widget.BaseAdapter.n_GetCount (intptr,intptr) <0x00037>
at (wrapper dynamic-method) object.672acf44-f214-4c4d-971c-dbdf21b47e10 (intptr,intptr) <0x00043>
Comment 1 Paul DiPietro [MSFT] 2015-11-04 18:10:43 UTC
Can you please provide a reproduction?
Comment 2 Caan Mee 2015-11-05 05:32:55 UTC
Since it's a pretty big project, and the error message is from one of our users (we dont have a galaxy tab as test device) I'll attach the relevant code, we think the issue could be reproduced:

  <ListView        
          VerticalOptions="FillAndExpand"
          ItemsSource="{Binding List}"		     
          Header="{Binding .}"
          Footer="{Binding .}"
          HasUnevenRows="True"
          IsGroupingEnabled="True"
          GroupDisplayBinding="{Binding Key}"
          SeparatorVisibility="None">

        <ListView.HeaderTemplate >
          <DataTemplate>
            <Label Text="Group Head"/>
          </DataTemplate>
        </ListView.HeaderTemplate>

        <ListView.FooterTemplate>
          <DataTemplate>
            <Label Text="Group Foot"/>
          </DataTemplate>
        </ListView.FooterTemplate>

        <ListView.GroupHeaderTemplate>
          <DataTemplate>
            <ViewCell>
			  <Label Text="{Binding Key}"/>
			</ViewCell>
          </DataTemplate>
        </ListView.GroupHeaderTemplate>

        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
			  <Label Text="{Binding .}"/>
			</ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>

      </ListView>
	  
	  
	 public class Grouping<K, T> : ObservableCollection<T>
    {
        public Grouping(K key, IEnumerable<T> items)
        {
            Key = key;
            foreach (var item in items)
            {
                Items.Add(item);
            }
        }

        public K Key { get; private set; }
    }
	
	public class ViewModel {
	   public ObservableCollection<Grouping<string, string>> List { get; set; }

		public ViewModel(){
		   List = new ObservableCollection<Grouping<string, string>>();		   
           List.Add(new Grouping<string, string>("1", new List<string> { "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3" }));
           List.Add(new Grouping<string, string>("2", new List<string> { "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3" }));
           List.Add(new Grouping<string, string>("3", new List<string> { "1", "2", "3", "1", "2", "3" , "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3" }));
		}
	}
Comment 3 Jason Smith [MSFT] 2016-04-13 19:51:25 UTC
Thank you
Comment 4 René 2016-06-01 13:42:57 UTC
Forms v2.2.0.45

By coincidence I got the same crash on Android (Samsung S5 neo). The ListView must be grouped, it does not happen for flat lists.

The full stack can be seen here: https://gist.github.com/Krumelur/4efdc7660b53454c701a22bb08c9c303
Comment 5 René 2016-06-01 13:52:30 UTC
More info: setting the ItemsSource from code prevents the crash.
Comment 6 Chris McBride 2017-03-13 22:58:21 UTC
I'm seeing almost the exact same stacktrace:
System.ArgumentOutOfRangeExceptionIndex was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Raw
System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)<f27039c598834ea7a7f946c865c2e2b7>:0
System.ThrowHelper.ThrowArgumentOutOfRangeException()<f27039c598834ea7a7f946c865c2e2b7>:0
System.Collections.Generic.List<T>.get_Item(int index)<f27039c598834ea7a7f946c865c2e2b7>:0
Cadenza.Collections.OrderedDictionary<TKey, TValue>.get_Item(int index)<b0fc14d4e5b04749b7241d1235a68329>:0
Xamarin.Forms.TemplatedItemsList<TView, TItem>.GetGroup(int index)<b0fc14d4e5b04749b7241d1235a68329>:0
Xamarin.Forms.TemplatedItemsList<TView, TItem>.GetGroup(int index)<b0fc14d4e5b04749b7241d1235a68329>:0
Xamarin.Forms.Platform.Android.ListViewAdapter.get_Count()<95895bcb439e42ebb8e7134a545f1acc>:0
Android.Widget.BaseAdapter.n_GetCount(IntPtr jnienv, IntPtr native__this)<4db2d40884194fdc8feb0df3089cee16>:0
at (wrapper dynamic-method) System.Object:26df19a0-6fa0-4e74-8bfe-646f879baeba (intptr,intptr)

It looks like there are some parallel arrays going on.
ListViewAdapter.get_Count does the following:
	for (var i = 0; i < templatedItems.Count; i++)
		count += templatedItems.GetGroup(i).Count;

This stacktrace is implying that these two lists are not in sync. Looking through the code I don't see how they can get out of sync, unless something is happening on another thread. Currently this is one of our top hits on Insights, so something is happening. A timing issue sounds plausible. If this is a timing issue, it will be nearly impossible to come up with a sample.
It would be better if we could some how get the count of groups rather than the count of templated items. But can we at least protect the code against an out of range exception?
Comment 7 Chris McBride 2017-03-14 00:43:04 UTC
Attaching a longer stack trace from a report on Google Play:
Caused by: android.runtime.JavaProxyThrowable: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
  at System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) [0x00011] in <f27039c598834ea7a7f946c865c2e2b7>:0 
  at System.ThrowHelper.ThrowArgumentOutOfRangeException () [0x00000] in <f27039c598834ea7a7f946c865c2e2b7>:0 
  at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x0000c] in <f27039c598834ea7a7f946c865c2e2b7>:0 
  at Cadenza.Collections.OrderedDictionary`2[TKey,TValue].get_Item (System.Int32 index) [0x00000] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.TemplatedItemsList`2[TView,TItem].GetGroup (System.Int32 index) [0x0000a] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.TemplatedItemsList`2[TView,TItem].Xamarin.Forms.ITemplatedItemsList<TItem>.GetGroup (System.Int32 index) [0x00000] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.Platform.Android.ListViewAdapter.get_Count () [0x00024] in <95895bcb439e42ebb8e7134a545f1acc>:0 
  at Android.Widget.BaseAdapter.n_GetCount (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <4db2d40884194fdc8feb0df3089cee16>:0 
  at (wrapper dynamic-method) System.Object:61e2ef83-4640-49d3-b843-04e84be48ff3 (intptr,intptr)
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <f27039c598834ea7a7f946c865c2e2b7>:0 
  at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x000a7] in <5d0da439400e4472845b738cfbb5a09c>:0 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeNonvirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00020] in <5d0da439400e4472845b738cfbb5a09c>:0 
  at Android.Widget.TextView.set_TextFormatted (Java.Lang.ICharSequence value) [0x00028] in <4db2d40884194fdc8feb0df3089cee16>:0 
  at Android.Widget.TextView.set_Text (System.String value) [0x00013] in <4db2d40884194fdc8feb0df3089cee16>:0 
  at Xamarin.Forms.Platform.Android.LabelRenderer.UpdateText () [0x00098] in <95895bcb439e42ebb8e7134a545f1acc>:0 
  at Xamarin.Forms.Platform.Android.LabelRenderer.OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x000c5] in <95895bcb439e42ebb8e7134a545f1acc>:0 
  at (wrapper delegate-invoke) <Module>:invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
  at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x00012] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.BindableObject+SetValueFlags attributes, System.Boolean silent) [0x000fb] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0014b] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x0020c] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.BindingExpression.Apply (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property) [0x0006b] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.Binding.Apply (System.Object newContext, Xamarin.Forms.BindableObject bindObj, Xamarin.Forms.BindableProperty targetProperty) [0x00042] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.BindableObject.ApplyBindings (System.Object oldContext, System.Boolean skipBindingContext) [0x0003b] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.BindableObject.ApplyBindings (System.Object oldContext) [0x00000] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject bindable, System.Object value) [0x0005a] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.Element.SetChildInheritedBindingContext (Xamarin.Forms.Element child, System.Object context) [0x00000] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.Element.OnBindingContextChanged () [0x00021] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.Cell.OnBindingContextChanged () [0x00000] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.BindableObject.BindingContextPropertyBindingPropertyChanged (Xamarin.Forms.BindableObject bindable, System.Object oldvalue, System.Object newvalue) [0x0001b] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.BindableObject+SetValueFlags attributes, System.Boolean silent) [0x0010e] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0014b] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0005f] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.BindableObject.set_BindingContext (System.Object value) [0x00000] in <b0fc14d4e5b04749b7241d1235a68329>:0 
  at Xamarin.Forms.TemplatedItemsList`2+<UnhookItem>d__153[TView,TItem].MoveNext () [0x000a7] in <b0fc14d4e5b04749b7241d1235a68329>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <f27039c598834ea7a7f946c865c2e2b7>:0 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (System.Object state) [0x00000] in <f27039c598834ea7a7f946c865c2e2b7>:0 
  at Android.App.SyncContext+<Post>c__AnonStorey0.<>m__0 () [0x00000] in <4db2d40884194fdc8feb0df3089cee16>:0 
  at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in <4db2d40884194fdc8feb0df3089cee16>:0 
  at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <4db2d40884194fdc8feb0df3089cee16>:0 
  at (wrapper dynamic-method) System.Object:b9e53a29-dfd4-49c3-9487-4a610241a14e (intptr,intptr)
	at mono.java.lang.RunnableImplementor.n_run(Native Method)
	at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
	at android.os.Handler.handleCallback(Handler.java:739)
	at android.os.Handler.dispatchMessage(Handler.java:95)
	at android.os.Looper.loop(Looper.java:135)
	at android.app.ActivityThread.main(ActivityThread.java:5333)
	... 4 more
Comment 8 Hines Vaughan III 2017-05-24 21:13:02 UTC
Possibly related bug that is said to have been fixed in 2.3.5-pre3:
https://bugzilla.xamarin.com/show_bug.cgi?id=51704
Comment 9 Hines Vaughan III 2017-05-24 21:25:50 UTC
Also setting ListViewCachingStrategy to RecycleElement seems like it may have fixed the issue for me, but I will update here if it happens again.
Comment 10 Samantha Houts [MSFT] 2017-05-25 19:26:21 UTC
@Hines Vaughan III: Good find! I believe you are correct. This is the same bug. Thank you, everyone!

*** This bug has been marked as a duplicate of bug 51704 ***
Comment 11 sh.naz 2017-11-09 14:19:55 UTC
Thanks a lot, @Hines Vaughan III. Setting ListViewCachingStrategy to RecycleElement does fix it.