Bug 46369 - Navigation.PopAsync() on the SelectedIndexChanged of a Picker causes a NullReferenceException in Xamarin.Forms Android
Summary: Navigation.PopAsync() on the SelectedIndexChanged of a Picker causes a NullRe...
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.3.4
Hardware: PC Windows
: Normal major
Target Milestone: ---
Assignee: Chris King
URL:
Depends on:
Blocks:
 
Reported: 2016-11-03 11:03 UTC by Jason Brown
Modified: 2017-05-05 17:29 UTC (History)
4 users (show)

Tags: popasync picker selectedindexchanged crash ac
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 Jason Brown 2016-11-03 11:03:20 UTC
On a new Xamarin.Forms project, updated to Xamarin.Forms v2.3.2.127, add the following to App.cs

public class App : Application
{
	public App()
	{
		MainPage = new NavigationPage(new MainPage());
	}
}

class MainPage : ContentPage
{
	public MainPage()
	{
		Title = "Main Page";
		var button = new Button() { Text = "Go to Picker Page", HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center };
		button.Clicked += async (object sender, EventArgs e) => await Navigation.PushAsync(new PickerPage());

		Content = button;
	}
}

class PickerPage : ContentPage
{
	public PickerPage()
	{
		Title = "Picker Page";

		var picker = new Picker() { HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, WidthRequest = 200, HeightRequest = 50, BackgroundColor = Color.Silver };
		picker.Items.Add("Item 1");
		picker.Items.Add("Item 2");

		picker.SelectedIndexChanged += async (object sender, EventArgs e) => 
										{
											// await Task.Delay(100);		// uncommenting this line stops Android from crashing
											await Navigation.PopAsync();
										};

		Content = picker;
	}

}

Select an item on the picker page and you'll get the following exception:-

11-03 10:58:01.194 I/MonoDroid( 2505): UNHANDLED EXCEPTION:
11-03 10:58:01.194 I/MonoDroid( 2505): System.NullReferenceException: Object reference not set to an instance of an object.
11-03 10:58:01.194 I/MonoDroid( 2505):   at Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer.<OnClick>b__9_2 (System.Object sender, System.EventArgs args) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\AppCompat\PickerRenderer.cs:113 
11-03 10:58:01.194 I/MonoDroid( 2505):   at Android.Content.IDialogInterfaceOnDismissListenerImplementor.OnDismiss (Android.Content.IDialogInterface dialog) [0x0000d] in /Users/builder/data/lanes/3819/5a02b032/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Android.Content.IDialogInterface.cs:369 
11-03 10:58:01.194 I/MonoDroid( 2505):   at Android.Content.IDialogInterfaceOnDismissListenerInvoker.n_OnDismiss_Landroid_content_DialogInterface_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_dialog) [0x00011] in /Users/builder/data/lanes/3819/5a02b032/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Android.Content.IDialogInterface.cs:334 
11-03 10:58:01.194 I/MonoDroid( 2505):   at (wrapper dynamic-method) System.Object:2a363bb9-db0e-4a55-9124-428b6f953d80 (intptr,intptr,intptr)


WORKROUND:

Adding 

await Task.Delay(100);

in the SelectedIndexChanged handler before calling await Navigation.PopAsync(); fixes the problem.
Comment 1 Paul DiPietro [MSFT] 2017-03-20 00:29:01 UTC
The crash does occur on Android (using the 2.3.5.71-nightly) although it appears to work on at least UWP. Didn't check iOS, though this would require further investigation.
Comment 2 Chris King 2017-05-05 17:29:35 UTC
Closing per Paul's comment. If it still repro's for you please zip/attach reproduction.