如何在 AlertDialog 中读取 RadioGroup 的值?
How do I read the value of my RadioGroup in an AlertDialog?
所以我正在使用 AlertDialog 来显示我的 RadioGroup,我想获取 RadioButton 的值以传递给我的下一个方法。然而,当我 运行 程序时它 returns 一个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'int
android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object
reference
我该如何解决这个问题?
private void showChoosePackage() {
AlertDialog.Builder builder = new AlertDialog.Builder(descriptionsActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View choose_package_layout = inflater.inflate(R.layout.choose_package_layout, null);
builder.setView(choose_package_layout);
final AlertDialog dialog = builder.create();
radioGroup = findViewById(R.id.radio_grp);
Button btnApply = choose_package_layout.findViewById(R.id.button_apply);
final RadioButton rdibtn = (RadioButton)choose_package_layout.findViewById(R.id.radio1);
final RadioButton rdibtn2 = (RadioButton)choose_package_layout.findViewById(R.id.radio2);
rdibtn.setText(Common.longhousedesc.PackageOne);
rdibtn2.setText(Common.longhousedesc.PackageTwo);
btnApply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int Selectedid = radioGroup.getCheckedRadioButtonId();
radioButton = findViewById(Selectedid);
showConfirmDialog(radioButton.getText());
}
});
builder.show();
}
查找单选组时需要指定视图:
radioGroup = choose_package_layout.findViewById(R.id.radio_grp);
findViewById
需要上下文才能找到视图。由于您是在对话框中调用它,因此您需要在其视图中找到该视图。
private void showChoosePackage() {
AlertDialog.Builder builder = new AlertDialog.Builder(descriptionsActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
final View choose_package_layout = inflater.inflate(R.layout.choose_package_layout, null);
builder.setView(choose_package_layout);
final AlertDialog dialog = builder.create();
final RadioGroup radioGroup = findViewById(R.id.radio_grp);
Button btnApply = choose_package_layout.findViewById(R.id.button_apply);
final RadioButton rdibtn = (RadioButton)choose_package_layout.findViewById(R.id.radio1);
final RadioButton rdibtn2 = (RadioButton)choose_package_layout.findViewById(R.id.radio2);
rdibtn.setText(Common.longhousedesc.PackageOne);
rdibtn2.setText(Common.longhousedesc.PackageTwo);
btnApply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int Selectedid = radioGroup.getCheckedRadioButtonId();
radioButton = choose_package_layout.findViewById(Selectedid);
showConfirmDialog(radioButton.getText());
}
});
dialog.show();
}
所以我正在使用 AlertDialog 来显示我的 RadioGroup,我想获取 RadioButton 的值以传递给我的下一个方法。然而,当我 运行 程序时它 returns 一个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference
我该如何解决这个问题?
private void showChoosePackage() {
AlertDialog.Builder builder = new AlertDialog.Builder(descriptionsActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View choose_package_layout = inflater.inflate(R.layout.choose_package_layout, null);
builder.setView(choose_package_layout);
final AlertDialog dialog = builder.create();
radioGroup = findViewById(R.id.radio_grp);
Button btnApply = choose_package_layout.findViewById(R.id.button_apply);
final RadioButton rdibtn = (RadioButton)choose_package_layout.findViewById(R.id.radio1);
final RadioButton rdibtn2 = (RadioButton)choose_package_layout.findViewById(R.id.radio2);
rdibtn.setText(Common.longhousedesc.PackageOne);
rdibtn2.setText(Common.longhousedesc.PackageTwo);
btnApply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int Selectedid = radioGroup.getCheckedRadioButtonId();
radioButton = findViewById(Selectedid);
showConfirmDialog(radioButton.getText());
}
});
builder.show();
}
查找单选组时需要指定视图:
radioGroup = choose_package_layout.findViewById(R.id.radio_grp);
findViewById
需要上下文才能找到视图。由于您是在对话框中调用它,因此您需要在其视图中找到该视图。
private void showChoosePackage() {
AlertDialog.Builder builder = new AlertDialog.Builder(descriptionsActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
final View choose_package_layout = inflater.inflate(R.layout.choose_package_layout, null);
builder.setView(choose_package_layout);
final AlertDialog dialog = builder.create();
final RadioGroup radioGroup = findViewById(R.id.radio_grp);
Button btnApply = choose_package_layout.findViewById(R.id.button_apply);
final RadioButton rdibtn = (RadioButton)choose_package_layout.findViewById(R.id.radio1);
final RadioButton rdibtn2 = (RadioButton)choose_package_layout.findViewById(R.id.radio2);
rdibtn.setText(Common.longhousedesc.PackageOne);
rdibtn2.setText(Common.longhousedesc.PackageTwo);
btnApply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int Selectedid = radioGroup.getCheckedRadioButtonId();
radioButton = choose_package_layout.findViewById(Selectedid);
showConfirmDialog(radioButton.getText());
}
});
dialog.show();
}