Bug 49786 - Listview crashing on KitKat
Summary: Listview crashing on KitKat
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.3
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Samantha Houts [MSFT]
Depends on:
Reported: 2016-12-13 02:00 UTC by Richard Long
Modified: 2017-06-19 23:13 UTC (History)
3 users (show)

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:

Description Richard Long 2016-12-13 02:00:55 UTC
I've isolated what I think is a bug running on KitKat. It seems *not* to crash in a simulator, strangely

Latest Stable of Xamarin.Forms, currently

I'm using a Xamarin forms listview, bound via XAML to a grouped collection.

I've found that if I update the collection directly I get the following error.

12-13 12:24:04.265 D/Mono    (20087): Found as 'java_interop_jnienv_throw'.
12-13 12:24:04.275 W/dalvikvm(20087): JNI WARNING: JNI function CallObjectMethodA called with exception pending
12-13 12:24:04.275 W/dalvikvm(20087):              in Lmd5b60ffeb829f638581ab2bb9b1a7f4f3f/ListViewAdapter;.n_getCount:()I (CallObjectMethodA)
12-13 12:24:04.275 W/dalvikvm(20087): Pending exception is:
12-13 12:24:04.275 I/dalvikvm(20087): android.runtime.JavaProxyThrowable: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
12-13 12:24:04.275 I/dalvikvm(20087): Parameter name: index
12-13 12:24:04.275 I/dalvikvm(20087):   at System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) [0x00000] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/throwhelper.cs:93 
12-13 12:24:04.275 I/dalvikvm(20087):   at System.ThrowHelper.ThrowArgumentOutOfRangeException () [0x00000] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/throwhelper.cs:56 
12-13 12:24:04.275 I/dalvikvm(20087):   at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x0000c] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/collections/generic/list.cs:181 
12-13 12:24:04.275 I/dalvikvm(20087):   at Cadenza.Collections.OrderedDictionary`2[TKey,TValue].get_Item (System.Int32 index) [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\OrderedDictionary.cs:101 
12-13 12:24:04.275 I/dalvikvm(20087):   at Xa
12-13 12:24:04.275 I/dalvikvm(20087): 	at md5b60ffeb829f638581ab2bb9b1a7f4f3f.ListViewAdapter.n_getCount(Native Method)
12-13 12:24:04.275 I/dalvikvm(20087): 	at md5b60ffeb829f638581ab2bb9b1a7f4f3f.ListViewAdapter.getCount(ListViewAdapter.java:44)
12-13 12:24:04.275 I/dalvikvm(20087): 	at android.widget.HeaderViewListAdapter.getCount(HeaderViewListAdapter.java:132)

As a hack-around, I've found that if I first re-initialise it, and then nest the update inside 
Device.StartTimer(new TimeSpan(0), () =>
	return false;

Then the app won't crash. The downside is that you get an ugly flash of no data in the listview during the update.

I've created an app to demonstrate the issue with a switch at the top to toggle what i've called "Race Condition Protection". Load more customers on a KitKat phone without this enabled and you'll get the above bug.
Funnily enough though, after you've loaded more customers using the race condition protection: Then you can turn it off and it'll work fine without the protection.

Steps to reproduce:
1) Use a phone running KitKat.
2) Fire up the attached project
3) without pressing the "Use Race Condition Protection" switch, click "Load more customers"
4) Crash.

1) Fire up the attached project
2) Press the "Use Race Condition Protection" switch
3) Click "Load more customers"
4) No Crash.
5) Remove "Use Race Condition Protection"
6) Click "Load more customers"
7) No Crash.
Comment 1 Richard Long 2016-12-20 03:56:48 UTC
Looks like my attachment didn't upload. I made another one, which crashes slightly differently. This one isn't as predictable, you can't just use the timer once and be okay: This one will continue to crash unpredicably on KitKat devices.
Comment 2 Samantha Houts [MSFT] 2016-12-27 19:50:29 UTC
@Richard Long: Can you try uploading your project again? Please omit the packages folder and the bin/obj folders to reduce the file size. Thanks!!
Comment 3 Samantha Houts [MSFT] 2017-06-19 23:13:59 UTC
Because we have not received a reply to our request for more information we are closing this issue. If you are still encountering this issue, please reopen the ticket with the requested information. Thanks!