Xamarin.Android。你调用的对象是空的。单击按钮

时间:2016-07-17 09:58:12

标签: 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 : 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);
    }
}

}

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>

为什么呢?这段代码出了什么问题?这段代码很简陋,但我无法解决问题。

1 个答案:

答案 0 :(得分:5)

您的代码没有任何问题。

在运行时,Android无法找到您的imageProfile视图,因此Resource.designer.cs将返回null。

尝试删除df.set_index(['A', 'B']) 的内容(不是文件)并重建。