Как получить значение выбранного радиокнопки из пяти радиокнопок в Android? - PullRequest
0 голосов
/ 01 декабря 2011

Мне нужно получить выбранное значение переключателя из 5 переключателей. Я сделал как:

Значения переключателей от db:

op1=(RadioButton)findViewById(R.id.option1);
op2=(RadioButton)findViewById(R.id.option2);
op3=(RadioButton)findViewById(R.id.option3);
op4=(RadioButton)findViewById(R.id.option4);
op5=(RadioButton)findViewById(R.id.option5);
System.out.println("after lv users ");
op1.setText(listItem.getOption1());
op2.setText(listItem.getOption2());
op3.setText(listItem.getOption3());
op4.setText(listItem.getOption4());
op5.setText(listItem.getOption5());

Действие проверки RadioButton:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup rg, int checkedId) {
 for(int i=0; i<radioGroup.getChildCount(); i++) {
 System.out.print("radiogroup child counts");
 System.out.print(radioGroup.getChildCount());
 op1 = (RadioButton) radioGroup.getChildAt(i);
 if(op1.isSelected()== true) {
  System.out.println("radio i value");
  System.out.print("option"+i);
  //String text = btn.getText();
  // do something with text

    }
  }
 }
}); 

Но это не работает. Ошибка NullPointerException в строке

op1.setText(listItem.getOption1());

Где я ошибся?

Помогите мне с этим.

1 Ответ

0 голосов
/ 01 декабря 2011

NullPointerException означает, что один из этих объектов (op1 или listItem) имеет значение null.

Попробуйте отладить код, чтобы найти нулевой объект.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...