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));
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…