当我按下任何按钮时,我得到:
System.NullReferenceException:未将对象引用设置为对象的实例.
MainActivity.cs
namespace Xamarin.Android.Pluralsight
{
[Activity(Label = "Xamarin.Android.Pluralsight", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private Button _buttonPrevious;
private Button _buttonNext;
private ImageView _imageProfile;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
_buttonPrevious = FindViewById<Button>(Resource.Id.buttonPrevious);
_buttonNext = FindViewById<Button>(Resource.Id.buttonNext);
_imageProfile = FindViewById<ImageView>(Resource.Id.imageProfile);
_buttonNext.Click += buttonNext_Click;
_buttonPrevious.Click += buttonPrevious_Click;
}
private void buttonPrevious_Click(object sender, EventArgs e)
{
_imageProfile.SetImageResource(Resource.Drawable.image1);
}
private void buttonNext_Click(object sender, EventArgs e)
{
_imageProfile.SetImageResource(Resource.Drawable.image2);
}
}
Run Code Online (Sandbox Code Playgroud)
}
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" …Run Code Online (Sandbox Code Playgroud)