android:单选按钮选项检索

时间:2011-11-21 11:03:18

标签: android radio-button

我在android中有一个包含3个选项的广播组。另外,我还有一个名为checked的按钮。当我点击“选中”按钮时,我想选择选项1。这就是我在做的事情:

RadioGroup options;
    RadioButton rb;
    TextView txt;
    CharSequence userMsg;
    Button checked;
    int id;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        options=(RadioGroup)findViewById(R.id.options);
        txt=(TextView)findViewById(R.id.txt);

options.setOnCheckedChangeListener(new android.widget.RadioGroup.OnCheckedChangeListener() {


            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                rb=(RadioButton)findViewById(checkedId);
                userMsg=rb.getText();
                txt.setText(userMsg);
            }
        });
checked.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {

        options.check(1);


    }
});


    }
}

它给我空指针异常错误。请帮助!

2 个答案:

答案 0 :(得分:1)

在按钮onClick功能

中写下此内容
tRadioButton.setChecked(true);

答案 1 :(得分:0)

Button checked已初始化为null

初始化:

Button checked = (button) findViewById(...);