0

我有一个实现 RadioGroupButton 和 CheckBox 的简单代码。

当应用程序在模拟器上启动时,没有异常,但是当我单击单选按钮时,它抛出了异常,我从 LogCat 注意到,有这个 NullPointerException 是由于在空对象引用上调用 getId() 引起的。

我已经查看了我的代码,但我不知道为什么会出现异常。这是我的代码,请帮助,非常感谢!

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
private RadioGroup GenderGroup;
private RadioButton rbMale;
private RadioButton rbFemale;
private CheckBox cbFootball;
private CheckBox cbBasketball;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GenderGroup = (RadioGroup)super.findViewById(R.id.gender);

    rbMale = (RadioButton)super.findViewById(R.id.male);
    rbMale = (RadioButton)super.findViewById(R.id.female);
    //System.out.printf("rbMale = %@", rbMale);
    //System.out.printf("rbFemale = %@", rbFemale);
    cbFootball = (CheckBox)super.findViewById(R.id.football);
    cbBasketball = (CheckBox)super.findViewById(R.id.basketball);

    // register OnCheckedChangeListener event:
    GenderGroup.setOnCheckedChangeListener(new GenderOnCheckedChangeListener());

    cbFootball.setOnCheckedChangeListener(new FootballOnCheckedChangeListener());

    cbBasketball.setOnCheckedChangeListener(new BasketballOnCheckedChangeListener());



}

private class GenderOnCheckedChangeListener implements OnCheckedChangeListener {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        String sGender = "";

        if (checkedId == rbFemale.getId()) {

            sGender = rbFemale.getText().toString();
        }

        if (checkedId == rbMale.getId()) {

            sGender = rbMale.getText().toString();
        }

        Toast.makeText(getApplicationContext(), "Your gender is :" + sGender, Toast.LENGTH_SHORT).show();

    }

}

private class FootballOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {

    @Override
    public void onCheckedChanged(CompoundButton button, boolean isChecked) {
        String sFav = "";
        if (cbFootball.isChecked()) {

            sFav = cbFootball.getText().toString(); 
            sFav = sFav + "selected!";
        }
        else {

            sFav = sFav + "not selected!";

        }

        Toast.makeText(getApplicationContext(), "Your favourite is: " + sFav, Toast.LENGTH_SHORT).show();
    }

}



private class BasketballOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener  {

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {

        String sFav = "";
        if (cbBasketball.isChecked()) {

            sFav = cbBasketball.getText().toString(); 
            sFav = sFav + "selected!";
        }
        else {

            sFav = sFav + "not selected!";
        }

        Toast.makeText(getApplicationContext(), "Your favourite is: " + sFav, Toast.LENGTH_SHORT).show();
    }

}

}

对不起,我还是新鲜人,不能贴图片。

4

2 回答 2

0

你应该用 rbFemale 替换第二个 rbMale

rbMale = (RadioButton)super.findViewById(R.id.male);
rbMale = (RadioButton)super.findViewById(R.id.female);
于 2014-11-16T19:33:44.083 回答
0

你没有初始化rbFemale。可能是复制粘贴错误。用这个:

rbMale = (RadioButton)super.findViewById(R.id.male);
rbFemale = (RadioButton)super.findViewById(R.id.female);
于 2014-11-16T19:35:41.550 回答