在AlertDialog[Android]中单击“正”按钮时崩溃
当我单击此警报对话框的肯定按钮时,应用程序崩溃。这个代码有什么问题吗?我感觉复选框分配不正确在AlertDialog[Android]中单击“正”按钮时崩溃,android,android-alertdialog,android-checkbox,Android,Android Alertdialog,Android Checkbox,当我单击此警报对话框的肯定按钮时,应用程序崩溃。这个代码有什么问题吗?我感觉复选框分配不正确 private void showSmimeSettings(){ AlertDialog.Builder smimeBuilder = new AlertDialog.Builder(this); smimeBuilder.setTitle("S/MIME Settings"); LayoutInflater inflator = getLayoutInflater();
private void showSmimeSettings(){
AlertDialog.Builder smimeBuilder = new AlertDialog.Builder(this);
smimeBuilder.setTitle("S/MIME Settings");
LayoutInflater inflator = getLayoutInflater();
View checkboxLayout = inflator.inflate(R.layout.smime_settings_checkbox, null);
smimeBuilder.setView(checkboxLayout);
final CheckBox signed_checkbox = (CheckBox) findViewById(R.id.signed_checkBox);
final CheckBox encrypted_checkbox = (CheckBox) findViewById(R.id.encrypted_checkBox);
smimeBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
if (signed_checkbox.isChecked()) {
mIsSigned = true;
} else {
mIsSigned = false;
}
if (encrypted_checkbox.isChecked()) {
mIsEncrypted = true;
} else {
mIsEncrypted = false;
}
}
});
AlertDialog smimeDialog = smimeBuilder.create();
smimeDialog.show();
}
日志:
我认为你应该在扩大布局后重新选中复选框
LayoutInflater inflator = getLayoutInflater();
View checkboxLayout = inflator.inflate(R.layout.smime_settings_checkbox, null);
final CheckBox signed_checkbox = (CheckBox) checkboxLayout.findViewById(R.id.signed_checkBox);
final CheckBox encrypted_checkbox = (CheckBox) checkBoxLayout.findViewById(R.id.encrypted_checkBox);
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/signed_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
/>
<CheckBox
android:id="@+id/encrypted_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
</LinearLayout>
尝试过,但仍然崩溃。已更新。基本上,这些复选框即使在赋值之后也是空的。你知道为什么它们会是空的吗?没有编译时错误,因此R.id.signed_复选框和R.id.encrypted_复选框已解决。您是否注意到checkboxLayout.findViewById(..)
中的checkboxLayout.
?请发布您更新的新代码。检查我的布局并与您的进行比较。是的,基本上是相同的。您解决了这个问题吗?我也面临同样的问题。
private void showSmimeSettings(){
AlertDialog.Builder smimeBuilder = new AlertDialog.Builder(this);
smimeBuilder.setTitle("S/MIME Settings");
LayoutInflater inflator = getLayoutInflater();
View checkboxLayout = inflator.inflate(R.layout.smime_settings_checkbox, null);
smimeBuilder.setView(checkboxLayout);
final CheckBox signed_checkbox = (CheckBox) checkboxLayout.findViewById(R.id.signed_checkBox);
final CheckBox encrypted_checkbox = (CheckBox) checkboxLayout.findViewById(R.id.encrypted_checkBox);
smimeBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
if (signed_checkbox.isChecked()) {
mIsSigned = true;
} else {
mIsSigned = false;
}
if (encrypted_checkbox.isChecked()) {
mIsEncrypted = true;
} else {
mIsEncrypted = false;
}
}
});
AlertDialog smimeDialog = smimeBuilder.create();
smimeDialog.show();
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/signed_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
/>
<CheckBox
android:id="@+id/encrypted_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
</LinearLayout>