我想从单选按钮获取文本但我收到错误
java.lang.NullPointerException:尝试调用虚方法' int android.widget.RadioGroup.getCheckedRadioButtonId()'在空对象引用上
请告诉我如何从单选按钮获取文本并删除def reduce[T[_]: Traversable, O: Monoid]: T[O] => O = { to =>
// This forces the compiler to pass in the correct implicits
val accumulate_ = accumulate[T, O, O]
accumulate_(a => a)(to)
}
。
这是代码
NullPointerException
这里是logcat
public class BookActivity extends AppCompatActivity {
EditText editText4;
EditText editText6;
RadioGroup time;
Button book;
RadioButton radioButton;
//RadioButton radioButton, radioButton2, radioButton3, radioButton4, radioButton5, radioButton6, radioButton7, radioButton8, radioButton9;
Context context = this;
UserDbHelper userDbHelper;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book);
editText4 = (EditText) findViewById(R.id.editText4);
editText6 = (EditText) findViewById(R.id.editText6);
time=(RadioGroup)findViewById(R.id.time);
book=(Button)findViewById(R.id.book);
addListenerOnButton();
}
public void addListenerOnButton() {
time = (RadioGroup) findViewById(R.id.radio);
book = (Button) findViewById(R.id.book);
book.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = time.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
if (radioButton != null) {
String pname = editText4.getText().toString();
String rtime = radioButton.getText().toString();
userDbHelper = new UserDbHelper(context);
sqLiteDatabase = userDbHelper.getWritableDatabase();
userDbHelper.addInformations(pname, rtime, sqLiteDatabase);
Toast.makeText(BookActivity.this, "Data Saved", Toast.LENGTH_LONG).show();
userDbHelper.close();
}
//Toast.makeText(BookActivity.this,radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:0)
问题可能出在RadioGroup Time Varaible上 因为首先将id设置为R.id.time并在addListener函数中将id更改为R.id.radio.check,即在xml中正确的RadioGroup Id。当然有这个帮助。谢谢