C# Xamarin.Android。对象引用未设置为对象的实例。点击按钮

C# Xamarin.Android。对象引用未设置为对象的实例。点击按钮,c#,android,xamarin,xamarin.android,C#,Android,Xamarin,Xamarin.android,当我按下任何按钮时,我得到: System.NullReferenceException:对象引用未设置为对象的实例 MainActivity.cs namespace Xamarin.Android.Pluralsight { [Activity(Label = "Xamarin.Android.Pluralsight", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Act

当我按下任何按钮时,我得到:

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);
    }
}
名称空间Xamarin.Android.Pluralsight
{
[活动(Label=“Xamarin.Android.Pluralsight”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
私人按钮_按钮前;
私人按钮_buttonNext;
私有ImageView _imageProfile;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
_buttonPrevious=FindViewById(Resource.Id.buttonPrevious);
_buttonNext=FindViewById(Resource.Id.buttonNext);
_imageProfile=FindViewById(Resource.Id.imageProfile);
_buttonNext.Click+=buttonNext\u Click;
_按钮上一步。单击+=按钮上一步\u单击;
}
私有无效按钮前一次单击(对象发送者,事件参数e)
{
_SetImageResource(Resource.Drawable.image1);
}
私有无效按钮下一步单击(对象发送者,事件参数e)
{
_SetImageResource(Resource.Drawable.image2);
}
}
}

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <Button
      android:id="@+id/buttonPrevious"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Previous"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true" />
  <Button
      android:id="@+id/buttonNext"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Next"
      android:layout_alignParentBottom="true"
      android:layout_alignParentRight="true" />
  <ImageView
      android:id="@+id/imageProfile"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:src="@drawable/image1" />
</RelativeLayout>


为什么??这个代码怎么了?这段代码是初步的,但我无法解决这个问题

您的代码没有问题

在运行时,Android找不到您的imageProfile视图,因此
FindViewById(Resource.Id.imageProfile)
返回null


尝试删除
Resource.designer.cs的内容(而不是文件)并重新生成。

谢谢您的建议。这很有帮助。它对我有用。