I am trying to make an android app using Xamarin so C#. I made two layouts and in each one of them I made tow buttons to navigate between them.I tried like this:
using System; using Android.App; using Android.OS; using Android.Views; using Android.Widget; namespace Example { [Activity(Label = "Example", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); this.SetContentView(Resource.Layout.Main); this.FindViewById<Button>(Resource.Id.ForwardButton).Click += this.Forward; this.FindViewById<Button>(Resource.Id.BackButton).Click += this.Back; } public void Forward(object sender, EventArgs e) { this.SetContentView(Resource.Layout.Main2); } public void Back(object sender, EventArgs e) { this.SetContentView(Resource.Layout.Main); } } } But every time when I start the app I get this errror: System.NullReferenceException has been thrown.Object reference not set to an instance of an object. Any advice or better idea?
Resourceisnull, or FindByViewId returned anullobject. Check it with your debugger. Assign the result ofFindViewByIdto its own variable if you have to, and check it fornullbefore dereferencing it.Resourcein Xamarin.Android is a designer-generated class similar toResourcesin winforms or WPF. It is a class reference, not an object reference so it can't be null.