Bug 22191 - On Android, NavigationPage.PushAsync() with Switch produces NullReferenceException
Summary: On Android, NavigationPage.PushAsync() with Switch produces NullReferenceExce...
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.2.2
Hardware: PC All
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2014-08-19 02:12 UTC by Brendan Zagaeski (Xamarin Team, assistant)
Modified: 2014-12-04 07:50 UTC (History)
6 users (show)

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

Test case (89.93 KB, application/zip)
2014-08-19 02:12 UTC, Brendan Zagaeski (Xamarin Team, assistant)

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 Brendan Zagaeski (Xamarin Team, assistant) 2014-08-19 02:12:13 UTC
Created attachment 7717 [details]
Test case

On Android, NavigationPage.PushAsync() with Switch on second page produces NullReferenceException after leaving and returning to the second page.

## Steps to reproduce

1. Run the FormsApp1.Android project from the attached test case on device or emulator.

2. Tap the full-screen "Go To Page 2" button.

3. Tap the back navigation button (the Android icon) in the top left corner of the screen.

4. Tap the full-screen "Go To Page 2" button again.

5. Tap the Switch widget.

## Result with Xamarin.Forms

> System.NullReferenceException: Object reference not set to an instance of an object
>  at Xamarin.Forms.Platform.Android.SwitchRenderer.HandleToggled (object,System.EventArgs) <IL 0x0000c, 0x00084>
>  at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.ToggledEventArgs>.invoke_void_object_TEventArgs (object,Xamarin.Forms.ToggledEventArgs) <0x0009f>
>  at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.ToggledEventArgs>.invoke_void_object_TEventArgs (object,Xamarin.Forms.ToggledEventArgs) <0x00067>
>  at Xamarin.Forms.Switch.<.cctor>b__0 (Xamarin.Forms.BindableObject,object,object) <IL 0x0001c, 0x00123>
>  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty,object,bool,bool,bool) <IL 0x000b9, 0x0058f>
>  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty,object,bool,bool,bool) <IL 0x00213, 0x00aa3>
>  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty,object,bool) <IL 0x0005a, 0x00277>
>  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty,object) <IL 0x00004, 0x00057>
>  at Xamarin.Forms.Switch.set_IsToggled (bool) <IL 0x0000c, 0x00083>
>  at Xamarin.Forms.Platform.Android.SwitchRenderer.Android.Widget.CompoundButton.IOnCheckedChangeListener.OnCheckedChanged (Android.Widget.CompoundButton,bool) <IL 0x00007, 0x00073>
>  at Android.Widget.CompoundButton/IOnCheckedChangeListenerInvoker.n_OnCheckedChanged_Landroid_widget_CompoundButton_Z (intptr,intptr,intptr,bool) [0x00011] in /Users/builder/data/lanes/1131/dc424d15/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.CompoundButton.cs:76
>  at (wrapper dynamic-method) object.f855b3dc-a845-41ed-b3ca-8c6ce2dbb337 (intptr,intptr,intptr,bool) <IL 0x0001d, 0x0004b>
> Could not find method java.lang.Throwable.<init>, referenced from method android.runtime.JavaProxyThrowable.<init>
> VFY: unable to resolve direct method 5583: Ljava/lang/Throwable;.<init> (Ljava/lang/String;Ljava/lang/Throwable;ZZ)V
> VFY: replacing opcode 0x70 at 0x0000

## Result with Xamarin.Forms 1.0.6186

No errors.

## Workaround

Instantiate a new `SwitchDemoPage()` on each call to `Navigation.PushAsync()`. That is, replace the following line:

> await ((Button)sender).Navigation.PushAsync(switchDemo);

... with:

> await ((Button)sender).Navigation.PushAsync(new SwitchDemoPage());

## Version information


Xamarin (0392433455d6cc9f503e2db4845a5889d394dc52)
Comment 2 Parmendra Kumar 2014-08-21 14:07:17 UTC
I have checked this issue and able to reproduce this, To reproduce this issue I
have followed the steps mentioned in bug description.
I observed that its work fine when we use:

await ((Button)sender).Navigation.PushAsync(new SwitchDemoPage());

Screencast: http://www.screencast.com/t/cylsWFJjfz

Application output: https://gist.github.com/Parmendrak/d10091753a96da044e21

Zip Xamarin Logs: https://gist.github.com/Parmendrak/761cf69c60f7371126af


Environment info:

Microsoft Visual Studio Professional 2013
Version 12.0.30723.00 Update 3
Microsoft .NET Framework
Version 4.5.51641

Xamarin (0a92fb56e49ed9add1c78e2c881ab6d71220d50f)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android
Comment 3 Eric Maupin 2014-09-16 20:52:32 UTC
Fixed for next update.
Comment 4 Indira 2014-12-04 07:50:49 UTC
I am developing the ios app using xamarin forms with stable channel on Mac os. I am facing the same issue with switch renderer. Is this issue fixed on stable channel?