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?
You are setting Main
as the layout for your activity and then in the following lines of code you are asking to find a button named Back
at runtime which is not part of this layout. This means the following line will return null:
FindViewById<Button>(Resource.Id.BackButton)
Now if you do FindViewById<Button>(Resource.Id.BackButton).Click
, you will definitely get a System.NullReferenceException
.
EDIT:
In view of the comments, here is what you should do to achieve what you are looking for:
Create two different activities (Main1
and Main2
). In Main1
you do:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
this.SetContentView(Resource.Layout.Main);
this.FindViewById<Button>(Resource.Id.ForwardButton).Click += this.Forward;
}
public void Forward(object sender, EventArgs e)
{
this.StartActivity (typeof(Main2));
}
Then in Main2
, you do:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
this.SetContentView(Resource.Layout.Main2);
this.FindViewById<Button>(Resource.Id.BackButton).Click += this.Back;
}
public void Back(object sender, EventArgs e)
{
this.StartActivity (typeof(Main));
}
Since it just happened to me and almost drove me up the wall, here is yet another way FindViewById
can return null
and how to fix it. Probably not a mistake a more experienced Android developer is likely to make, but still, it might prevent some frustration for someone like me in the future.
My control was in the correct view (not a fragment), the ID was assigned correctly, everything used to work just a couple of days ago when I last worked on the project.
After building the project, the very first FindViewById
call in OnCreate
of my main view suddenly returned null
when run on a physical device
It turns out I had just run some updates through the Android SDK Manager, which introduced a new Android Version (in this case Android 7) to my system, and my device simply did not have that version installed yet.
null
return value.TL;DR Check that the version of Android you are compiling against is in fact supported by the device you are testing on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With