使用自定义按钮xamarin的NullReferenceException

时间:2017-05-23 14:53:38

标签: button xamarin nullreferenceexception

所以我是xamarin / c#的新手,所以这可能很简单! 我试图制作一个能够在网球比赛中得分的应用程序。但我一直在努力 我模拟应用程序并启动调试器后,ButtonRood.Click + = delegate {}函数中出现NullReferenceException错误。在模拟器中,只显示标题栏,因此SetContentView无法正常工作,但我认为这是由于NullReferenceException。

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

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        int n_clicked_rood = 0;
        int n_clicked_blauw = 0;
        int game_rood = 0;
        int game_blauw = 0;
        bool Rood_Deuce = false;
        bool Blauw_Deuce = false;

        Button ButtonRood = FindViewById<Button>(Resource.Id.ButtonRood);
        Button ButtonBlauw = FindViewById<Button>(Resource.Id.ButtonBlauw);
        ProgressBar TijdBar = FindViewById<ProgressBar>(Resource.Id.TijdBar);
        EditText Rood1 = FindViewById<EditText>(Resource.Id.Rood1);
        EditText Rood2 = FindViewById<EditText>(Resource.Id.Rood2);
        EditText Rood3 = FindViewById<EditText>(Resource.Id.Rood3);
        EditText Rood4 = FindViewById<EditText>(Resource.Id.Rood4);
        EditText Blauw1 = FindViewById<EditText>(Resource.Id.Blauw1);
        EditText Blauw2 = FindViewById<EditText>(Resource.Id.Blauw2);
        EditText Blauw3 = FindViewById<EditText>(Resource.Id.Blauw3);
        EditText Blauw4 = FindViewById<EditText>(Resource.Id.Blauw4);
        EditText TextRood = FindViewById<EditText>(Resource.Id.TextRood);
        EditText TextBlauw = FindViewById<EditText>(Resource.Id.TextBlauw);


        ButtonRood.Click += delegate
        {
            //So is deuce do something completely different, probably -1 blauw +1 rood instead of +1 all the time
            // 
            if ((Rood_Deuce == true) && (Blauw_Deuce == true)) {
                n_clicked_rood += 1; n_clicked_blauw -= 1;
                if (n_clicked_rood > n_clicked_blauw) { Rood1.Text = "AD"; Blauw1.Text = "0"; }
                if (n_clicked_rood == n_clicked_blauw) { Rood1.Text = "40"; Blauw1.Text = "40"; }
            }

            else {
                n_clicked_rood += 1;
                if (n_clicked_rood == 1) { Rood1.Text = "15"; }
                if (n_clicked_rood == 2) { Rood1.Text = "30"; }
                if (n_clicked_rood == 3) { Rood1.Text = "40"; Rood_Deuce = true; }
                if (n_clicked_rood == 4) {
                    Rood1.Text = "0"; Blauw1.Text = "0";
                    game_rood += 1; Rood2.Text = Convert.ToString(game_rood);
                    n_clicked_blauw = 0; n_clicked_rood = 0;}
            }

        };

        ButtonBlauw.Click += delegate
        {

            if ((Rood_Deuce == true) && (Blauw_Deuce == true))
            {
                n_clicked_rood -= 1; n_clicked_blauw += 1;
                if (n_clicked_rood > n_clicked_blauw) { Rood1.Text = "AD"; Blauw1.Text = "0"; }
                if (n_clicked_rood == n_clicked_blauw) { Rood1.Text = "40"; Blauw1.Text = "40"; }
            }
            else
            {
                n_clicked_blauw += 1;
                if (n_clicked_blauw == 1) { Blauw1.Text = "15"; }
                if (n_clicked_blauw == 2) { Blauw1.Text = "30"; }
                if (n_clicked_blauw == 3) { Blauw1.Text = "40"; Blauw_Deuce = true; }
                if (n_clicked_blauw == 4)
                {
                    Blauw1.Text = "0"; Rood1.Text = "0";
                    n_clicked_blauw = 0; n_clicked_rood = 0;
                    game_blauw += 1; Blauw2.Text = Convert.ToString(game_blauw);
                }
            }

        };

    }
}

}

0 个答案:

没有答案