2011-12-01 64 views
0

我需要從5個單選按鈕中選取所選的單選按鈕值。我曾經做過這樣的:如何從android中的五個單選按鈕獲取選定的單選按鈕值?

單選按鈕值從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()); 

單選檢查行動:

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 

    } 
    } 
} 
}); 

但它不是working.It的示值誤差爲NullPointerException在line

op1.setText(listItem.getOption1()); 

我哪裏出錯了?

幫我解決這個問題。

回答

-1

NullPointerException表示其中一個對象(op1listItem)爲空。

嘗試調試您的代碼以查找空對象。

相關問題