Bug 21618 - Android - ListView reappearing and selecting its item causes jobject Exception
Summary: Android - ListView reappearing and selecting its item causes jobject Exception
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.2.2
Hardware: Other Other
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-07-26 09:15 UTC by daniel.luberda
Modified: 2015-03-19 17:32 UTC (History)
4 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 FIXED

Description daniel.luberda 2014-07-26 09:15:35 UTC
Steps: 
1. Click MAIN MENU button
2. Press back button
3. Click MAIN MENU button again
4. Select one of ListView's items

Result:
System.Diagnostics.Debugger.Mono_UnhandledException (ex={System.ArgumentException: 'jobject' must not be IntPtr.Zero.
Parameter name: jobject
at Android.Runtime.JNIEnv.CallIntMethod (intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/7c475463/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:342
at Android.Widget.AdapterView.get_FirstVisiblePosition () [0x00043] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/7c475463/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.AdapterView.cs:866
at Xamarin.Forms.Platform.Android.ListViewAdapter.SelectItem (object) <IL 0x0001f, 0x00080>
at Xamarin.Forms.Platform.Android.ListViewAdapter.OnItemSelected (object,Xamarin.Forms.SelectedItemChangedEventArgs) <IL 0x00017, 0x00077>
at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.SelectedItemChangedEventArgs>.invoke_void_object_TEventArgs (object,Xamarin.Forms.SelectedItemChangedEventArgs) <0x0007b>
at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.SelectedItemChangedEventArgs>.invoke_void_object_TEventArgs (object,Xamarin.Forms.SelectedItemChangedEventArgs) <0x0003f>
at Xamarin.Forms.ListView.OnSelectedItemChanged (Xamarin.Forms.BindableObject,object,object) <IL 0x0001c, 0x000b7>
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty,object,bool,bool,bool) <IL 0x000b9, 0x0032e>
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty,object,bool,bool,bool) <IL 0x00213, 0x007cf>
at Xamarin.Forms.ListView.NotifyRowTapped (int,int) <IL 0x0004b, 0x0015b>
at Xamarin.Forms.ListView.NotifyRowTapped (int) <IL 0x00025, 0x000c7>
at Xamarin.Forms.Platform.Android.ListViewAdapter.OnRealItemClicked (object,Android.Widget.AdapterView/ItemClickEventArgs) <IL 0x00025, 0x000bb>
at Android.Widget.AdapterView/IOnItemClickListenerImplementor.OnItemClick (Android.Widget.AdapterView,Android.Views.View,int,long) [0x0000d] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/7c475463/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.AdapterView.cs:261
at Android.Widget.AdapterView/IOnItemClickListenerInvoker.n_OnItemClick_Landroid_widget_AdapterView_Landroid_view_View_IJ (intptr,intptr,intptr,intptr,int,long) [0x00019] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/7c475463/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.AdapterView.cs:194
at (wrapper dynamic-method) object.c0b1c0fb-b2fb-42de-92cb-a13d4836dd18 (intptr,intptr,intptr,intptr,int,long) <IL 0x00029, 0x00047>
}) in 

Code:
public class ListViewBug : ContentPage
{
	public ObservableCollection<string> Collection = new ObservableCollection<string>();

	public ListViewBug()
	{
		for (int i = 0; i < 20; i++)
		{
			Collection.Add(DateTime.Now.ToString());
		}

		var listView = new ListView() {
			HorizontalOptions = LayoutOptions.FillAndExpand,
			HasUnevenRows = true, 
			ItemsSource = Collection,
		};

		listView.ItemSelected += (sender, e) => 
		{
			listView.SelectedItem = null;
		};

		var root = new StackLayout() {
			Padding = 5,
			Spacing = 5,
			HorizontalOptions = LayoutOptions.FillAndExpand,
			VerticalOptions = LayoutOptions.FillAndExpand,
			Children = {
				listView
			}
		};

		Content = root;
	}
}

public class SimpleMainMenu : MasterDetailPage
{
	ListViewBug secondPage = new ListViewBug();

	public SimpleMainMenu()
	{
		var button = new Button() {
			Text = "MAIN MENU BUTTON"
		};

		button.Clicked += (sender, e) => 
		{
			Navigation.PushAsync(secondPage);
		};

		Master = new ContentPage() {
			Title = "Master"
		};

		Detail = new ContentPage() {
			Title = "Detail",
			Content = button
		};
	}
}
Comment 1 daniel.luberda 2014-07-26 11:06:16 UTC
Another one:

$exception	{System.ArgumentException: 'jobject' must not be IntPtr.Zero. Parameter name: jobject at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/7c475463/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:866 at Android.Views.View.RequestLayout () [0x00058] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/7c475463/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.View.cs:14565 at Xamarin.Forms.Platform.Android.VisualElementTracker.HandleRedrawNeeded (object,Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>) <IL 0x00054, 0x00313> at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>>.invoke_void_object_TEventArgs (object,Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>) <0x00067> at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>>.invoke_void_object_TEventArgs (object,Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>) <0x00067> at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>>.invoke_void_object_TEventArgs (object,Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>) <0x00067> at Xamarin.Forms.VisualElement.BatchCommit () <IL 0x00031, 0x0012f> at Xamarin.Forms.VisualElement.set_Bounds (Xamarin.Forms.Rectangle) <IL 0x00072, 0x00363> at Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle) <IL 0x00002, 0x000cb> at Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion (Xamarin.Forms.VisualElement,Xamarin.Forms.Rectangle) <IL 0x00118, 0x008db> at Xamarin.Forms.Page.LayoutChildren (double,double,double,double) <IL 0x00105, 0x008c3> at Xamarin.Forms.Page.UpdateChildrenLayout () <IL 0x000ad, 0x00583> at Xamarin.Forms.Page.OnSizeAllocated (double,double) <IL 0x00010, 0x000ab> at Xamarin.Forms.VisualElement.SizeAllocated (double,double) <IL 0x00003, 0x00083> at Xamarin.Forms.VisualElement.SetSize (double,double) <IL 0x00024, 0x00173> at Xamarin.Forms.VisualElement.set_Bounds (Xamarin.Forms.Rectangle) <IL 0x0006c, 0x00347> at Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle) <IL 0x00002, 0x000cb> at Xamarin.Forms.Platform.Android.Platform.OnLayout (bool,int,int,int,int) <IL 0x0005b, 0x002fb> at Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout (bool,int,int,int,int) <IL 0x0001b, 0x000d3> at Android.Views.ViewGroup.n_OnLayout_ZIIII (intptr,intptr,bool,int,int,int,int) [0x00009] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/7c475463/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.ViewGroup.cs:3275 at (wrapper dynamic-method) object.56e2d42b-6c8a-41e9-86ce-0d515f6649a5 (intptr,intptr,bool,int,int,int,int) <IL 0x0002f, 0x00093> }	System.ArgumentException
Comment 2 daniel.luberda 2014-07-30 19:08:53 UTC
There's another reproduction which produces similar exception triggered when updating ListView Item Source (may be a different bug). 

Steps: 
1. Click "Page1" button
2. Type something into SearchBox
3. Press back to return to the main menu
4. Click "Page1" button
5. Type something into SearchBox

Result: 
$exception	{System.ArgumentException: 'jobject' must not be IntPtr.Zero. Parameter name: jobject at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/7c475463/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:866 at Android.Views.View.RequestLayout () [0x00058] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/7c475463/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.View.cs:14565 at Xamarin.Forms.Platform.Android.VisualElementTracker.HandleRedrawNeeded (object,Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>) <IL 0x00054, 0x00313> at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>>.invoke_void_object_TEventArgs (object,Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>) <0x00067> at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>>.invoke_void_object_TEventArgs (object,Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>) <0x00067> at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>>.invoke_void_object_TEventArgs (object,Xamarin.Forms.EventArg`1<Xamarin.Forms.VisualElement>) <0x00067> at Xamarin.Forms.VisualElement.BatchCommit () <IL 0x00031, 0x0012f> at Xamarin.Forms.VisualElement.set_Bounds (Xamarin.Forms.Rectangle) <IL 0x00072, 0x00363> at Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle) <IL 0x00002, 0x000cb> at Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion (Xamarin.Forms.VisualElement,Xamarin.Forms.Rectangle) <IL 0x00118, 0x008db> at Xamarin.Forms.Page.LayoutChildren (double,double,double,double) <IL 0x00105, 0x008c3> at Xamarin.Forms.Page.UpdateChildrenLayout () <IL 0x000ad, 0x00583> at Xamarin.Forms.Page.OnSizeAllocated (double,double) <IL 0x00010, 0x000ab> at Xamarin.Forms.VisualElement.SizeAllocated (double,double) <IL 0x00003, 0x00083> at Xamarin.Forms.VisualElement.SetSize (double,double) <IL 0x00024, 0x00173> at Xamarin.Forms.VisualElement.set_Bounds (Xamarin.Forms.Rectangle) <IL 0x0006c, 0x00347> at Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle) <IL 0x00002, 0x000cb> at Xamarin.Forms.Platform.Android.Platform.OnLayout (bool,int,int,int,int) <IL 0x0005b, 0x002fb> at Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout (bool,int,int,int,int) <IL 0x0001b, 0x000d3> at Android.Views.ViewGroup.n_OnLayout_ZIIII (intptr,intptr,bool,int,int,int,int) [0x00009] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/7c475463/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.ViewGroup.cs:3275 at (wrapper dynamic-method) object.21359bda-29ed-4127-9d20-2e6133df6d0f (intptr,intptr,bool,int,int,int,int) <IL 0x0002f, 0x00093> }	System.ArgumentException

CODE:

namespace XamarinTest
{
	public class SimpleMainMenu : ContentPage
	{
		XamarinListViewBug page1 = new XamarinListViewBug();

		public SimpleMainMenu()
		{
			var button1 = new Button() {
				Text = "PAGE1"
			};

			button1.Clicked += (sender, e) => 
			{
				Navigation.PushAsync(page1);
			};
				
			var root = new StackLayout() {
				HorizontalOptions = LayoutOptions.FillAndExpand,
				VerticalOptions = LayoutOptions.FillAndExpand,
				Children = {
					button1,
				}
			};

			Content = root;
		}
	}
	
		public class XamarinListViewBug : MasterDetailPage
	{
		bool flag;

		ObservableCollection<Tuple<string, string>> collection = new ObservableCollection<Tuple<string, string>>();

		void FillTheList()
		{
			collection.Clear();

			for (int i = 0; i < 100; i++)
			{
				var item = new Tuple<string, string>(
					string.Format("{0} {0} {0} {0} {0} {0}", flag ? i : 100-i),
					string.Format("---- i ----{0} {0} {0} {0} {0} {0}", flag ? i : 100-i)
				);


				collection.Add(item);
			}

			flag = !flag;
		}

		public XamarinListViewBug()
		{
			Title = "XamarinListViewBug";

			SearchBar search = new SearchBar();
			search.SearchButtonPressed += (sender, e) => FillTheList();

			ListView list = new ListView() {
				HorizontalOptions = LayoutOptions.FillAndExpand,
				VerticalOptions = LayoutOptions.FillAndExpand,
				HasUnevenRows = true, 
				ItemsSource = collection,
				ItemTemplate = new DataTemplate(typeof(CellTemplate)) 
			};

			StackLayout root = new StackLayout() {
				HorizontalOptions = LayoutOptions.FillAndExpand,
				VerticalOptions = LayoutOptions.FillAndExpand,
				Children =
				{
					search,
					list,
				}
			};

			Master = new ContentPage()
			{
				Title = "Master"
			};
			Detail = new ContentPage()
			{
				Title = "Detail",
				Content = root
			};
		}

		class CellTemplate : ViewCell
		{
			public CellTemplate()
			{
				Label cellLabel = new Label() {
					HorizontalOptions = LayoutOptions.FillAndExpand,
				};

				cellLabel.SetBinding(Label.TextProperty, new Binding("Item1", BindingMode.OneWay));

				StackLayout root = new StackLayout() {
					Children = 
					{
						cellLabel
					}
				};

				View = root;
			}
		}
	}
}
Comment 3 Eric Maupin 2014-08-06 13:32:09 UTC
Fixed for next update.
Comment 4 Eric Maupin 2014-08-06 14:31:25 UTC
Just to note, the two additional exceptions are separate issues from the original and each other.
Comment 5 Eric Maupin 2014-08-06 18:50:14 UTC
The crash from comment #2 has also been fixed for the next update (it had to do with the MasterDetailPage, actually). If you can file the second one separately with a reproduction I will look into it.
Comment 6 D F 2015-03-19 17:32:51 UTC
Apparently this bug is happening in version 1.4.0.6341. The updated code from the bug description can be found below.
The only change is: 

//				Navigation.PushAsync (secondPage); //commented out
				Detail.Navigation.PushAsync (secondPage); //updated line

------------------------------------------------------------------


	public class ListViewBug : ContentPage
	{
		public ObservableCollection<string> Collection = new
			ObservableCollection<string> ();

		public ListViewBug ()
		{
			for (int i = 0; i < 20; i++)
			{
				Collection.Add (DateTime.Now.ToString ());
			}

			var listView = new ListView () {
				HorizontalOptions = LayoutOptions.FillAndExpand,
				HasUnevenRows = true, 
				ItemsSource = Collection,
			};

			listView.ItemSelected += (sender, e) =>
			{
				listView.SelectedItem = null;
			};

			var root = new StackLayout () {
				Padding = 5,
				Spacing = 5,
				HorizontalOptions = LayoutOptions.FillAndExpand,
				VerticalOptions = LayoutOptions.FillAndExpand,
				Children = {
					listView
				}
			};

			Content = root;
		}
	}

	public class SimpleMainMenu : MasterDetailPage
	{
		ListViewBug secondPage = new ListViewBug ();

		public SimpleMainMenu ()
		{
			var button = new Button () {
				Text = "MAIN MENU BUTTON"
			};

			button.Clicked += (sender, e) =>
			{
//				Navigation.PushAsync (secondPage);
				Detail.Navigation.PushAsync (secondPage);
			};

			Master = new ContentPage () {
				Title = "Master"
			};

			Detail = new NavigationPage (new ContentPage {
				Title = "Detail",
				Content = button
			});
		}
	}


I can't get the exception itself for some reason (the app just crashes and Xamarin Studio stops debugging), but here's the Call Stack:

System.Diagnostics.Debugger.Mono_UnhandledException_internal () in 
System.Diagnostics.Debugger.Mono_UnhandledException (ex=) in 
object.0376e553-b528-4433-a597-f537683626a4 (Parameters=) in 
Android.Runtime.JNIEnv.CallIntMethod (Parameters=) in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/9e05e39f/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:343
Android.Widget.AdapterView.get_FirstVisiblePosition (Parameters=) in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/9e05e39f/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Widget.AdapterView.cs:890
Xamarin.Forms.Platform.Android.ListViewAdapter.SelectItem (Parameters=) in 
Xamarin.Forms.Platform.Android.ListViewAdapter.OnItemSelected (Parameters=) in 
Xamarin.Forms.ListView.OnSelectedItemChanged (Parameters=) in 
Xamarin.Forms.BindableObject.SetValueActual (Parameters=) in 
Xamarin.Forms.BindableObject.SetValueCore (Parameters=) in 
Xamarin.Forms.BindableObject.SetValueCore (Parameters=) in 
Xamarin.Forms.ListView.NotifyRowTapped (Parameters=) in 
Xamarin.Forms.ListView.NotifyRowTapped (Parameters=) in 
Xamarin.Forms.Platform.Android.ListViewAdapter.HandleItemClick (Parameters=) in 
Xamarin.Forms.Platform.Android.CellAdapter.OnItemClick (Parameters=) in 
Android.Widget.AdapterView.IOnItemClickListenerInvoker.n_OnItemClick_Landroid_widget_AdapterView_Landroid_view_View_IJ (Parameters=) in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/9e05e39f/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Widget.AdapterView.cs:194
object.0376e553-b528-4433-a597-f537683626a4 (Parameters=) in