Reputation: 633
I'm new to Xamarin.Forms.
I got an app that works well on iOS. However, on Android, it crashes after some time, and throws the following error:
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object.
Debug says it happens on this code:
private void OnElementToggled(object sender, EventArgs e)
{
this.Element.IsToggled = this.Control.Checked;
}
This toggle turns an option on or off inside our app. The toggle works fine on iOS. It also works on Android, but if I navigate around the app and switch the toggle on/off a few times, I get the error. I only get this error on Android, and only after I navigate around. Also, I get it at different times on simulator vs device (Galaxy S5 Neo). The simulator can run longer before I get the error.
I'm dumbfounded. How do I fix this?
I've searched and found What is a NullReferenceException, and how do I fix it?. That solution doesn't seem to apply in my case, because my code works fine on iOS and initially on Android.
Thank you very much for your time and help.
Upvotes: 0
Views: 638
Reputation: 89214
Try/Catch is a basic C# concept, any intro book will cover it
private void OnElementToggled(object sender, EventArgs e)
{
try {
this.Element.IsToggled = this.Control.Checked;
catch (Exception ex) {
// use logging (ie, appcenter.ms) to log this exception
}
}
Upvotes: 1