Android 我的单选按钮有问题

Android 我的单选按钮有问题,android,if-statement,Android,If Statement,我在广播组创建了两个小组,其中一个问你足球踢得好吗?另一个是关于篮球的。我给了3分,如果他们是好的,我想在我的程序中计算总分,但我不能。我需要帮助 private void addListenerOnButton() { final RadioGroup football = (RadioGroup) findViewById(R.id.radioGroup1); Button calc = (Button) findViewById(R.id.diabetriskbutton1

我在广播组创建了两个小组,其中一个问你足球踢得好吗?另一个是关于篮球的。我给了3分,如果他们是好的,我想在我的程序中计算总分,但我不能。我需要帮助

private void addListenerOnButton() {
    final RadioGroup football = (RadioGroup) findViewById(R.id.radioGroup1);
    Button calc = (Button) findViewById(R.id.diabetriskbutton1);
    final RadioGroup basketball = (RadioGroup) findViewById(R.id.radioGroup2);
    calc.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            int IDfootball = football.getCheckedRadioButtonId();
            RadioButton football = (RadioButton) findViewById(IDfootball);
            if (IDfootball == R.id.item1yes) {
                int scorefootball = 3;

            } else if (IDfootball == R.id.item1no) { 
                int scorefootball = 2;

            } 

            int IDbasketball = basketball.getCheckedRadioButtonId();
            RadioButton basketball = (RadioButton) findViewById(IDbasketball);
            if (IDbasketball == R.id.item1yes) {
                int scorebasketball = 3;

            } else if (IDbasketball == R.id.item1no) { 
                int scorebasketball = 2;

            } 
            scoretotal = scorefootball + scorebasketball ;


        }
    });

}`  

尝试删除这些行。我认为您的编译器将RadioGroup football | basketball变量与onClick方法中相同的局部变量混淆了

我还认为它们是不必要的,因为它们在申报后从未使用过。(您已经在将从getCheckedRadioButtonId()获得的ID与R.ID.itemYes | No进行比较

        RadioButton football = (RadioButton) findViewById(IDfootball);

        RadioButton basketball = (RadioButton) findViewById(IDbasketball);
另一点。您在if和else块内声明了scorefootball和scorebasketball。编译器将不会在各自块外看到这些变量。 此行将抛出一个错误:

        scoretotal = scorefootball + scorebasketball;
请在类似区域外申报:

        int scorefootball = 2;//default value
        int scorebasketball = 2;//default value

        ...

        if (IDfootball == R.id.item1yes) {
            scorefootball = 3;

        } else if (IDfootball == R.id.item1no) { 
            scorefootball = 2;

        } 

        ...

        if (IDbasketball == R.id.item1yes) {
            scorebasketball = 3;

        } else if (IDbasketball == R.id.item1no) { 
            scorebasketball = 2;

        }

        scoretotal = scorefootball + scorebasketball ;

您还可以发布您的xml吗?我想知道为什么您要将从不同的RadioGroup获取的ID与相同的R.ID.item1Yes | No

尝试删除这些行。我认为您的编译器会将RadioGroup football | basketball变量与onClick方法中相同的局部变量混淆

我还认为它们是不必要的,因为它们在声明之后从未被使用过。(您已经将从getCheckedRadioButtonId()获得的ID与R.ID.itemYes | No进行了比较。)

        RadioButton football = (RadioButton) findViewById(IDfootball);

        RadioButton basketball = (RadioButton) findViewById(IDbasketball);
另一点。您在if和else块内声明了scorefootball和scorebasketball。编译器将不会在各自块外看到这些变量。 此行将抛出一个错误:

        scoretotal = scorefootball + scorebasketball;
请在类似区域外申报:

        int scorefootball = 2;//default value
        int scorebasketball = 2;//default value

        ...

        if (IDfootball == R.id.item1yes) {
            scorefootball = 3;

        } else if (IDfootball == R.id.item1no) { 
            scorefootball = 2;

        } 

        ...

        if (IDbasketball == R.id.item1yes) {
            scorebasketball = 3;

        } else if (IDbasketball == R.id.item1no) { 
            scorebasketball = 2;

        }

        scoretotal = scorefootball + scorebasketball ;
您还可以发布您的xml吗?我想知道您为什么要将从不同放射组的getCheckedRadioButtonId获得的ID与相同的R.ID.item1Yes | No进行比较