为什么在Xamarin Android中创建自定义警报时抛出未设置为对象实例的对象引用?

时间:2016-12-14 01:34:16

标签: android xamarin

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

         SetContentView (Resource.Layout.Main);
        var btn = FindViewById<Button>(Resource.Id.button1);
        btn.Click += delegate
          {
              var popup = FindViewById<LinearLayout>(Resource.Id.linearLayout1);

              var btncamera = popup.FindViewById<Button>(Resource.Id.btnCamera);
              AlertDialog.Builder builder = new AlertDialog.Builder(this);
              builder.SetView(Resource.Layout.popup);
              builder.Show();

              btncamera.Click += delegate
                {
                    builder.Dispose();
                };

          };

    }

谢谢

enter image description here

1 个答案:

答案 0 :(得分:0)

你做错了

所以改变你的代码

btn.Click += delegate
          {
              var popup = FindViewById<LinearLayout>(Resource.Id.linearLayout1);

              var btncamera = popup.FindViewById<Button>(Resource.Id.btnCamera);
              AlertDialog.Builder builder = new AlertDialog.Builder(this);
              builder.SetView(Resource.Layout.popup);
              builder.Show();

              btncamera.Click += delegate
                {
                    builder.Dispose();
                };

          };

这样

btn.Click += delegate
          {
              AlertDialog.Builder builder = new AlertDialog.Builder(this);
              LayoutInflater inflater = this.LayoutInflater;
              var dialogView = inflater.inflate(Resource.Layout.linearLayout1, null);
              builder.SetView(dialogView);
              var btncamera = builder.FindViewById<Button>(Resource.Id.btnCamera);
              builder.Show();

              btncamera.Click += delegate
                {
                    builder.Dispose();
                };

          };