我创建了警报对话框(底部带有复选框),并在单击首选项时启动它。第一次成功启动警报对话框,但第二次出现以下异常:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
MainActivity.java
final View checkBoxView = View.inflate(this, R.layout.checkbox, null); /*to inflate dialog with checkbox */
CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
System.out.println("******** CheckBox Clicked *********");
/* Save to shared preferences */
}
});
checkBox.setText("Use by default for this action");
Preference pref = (Preference) findPreference("preference");
pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
final CharSequence[] items = {"BT Smart Settings by ABC",
"BT Smart Settings by XYZ",
"BT Smart Settings by DEFAULT"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Complete action using");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
System.out.println("************* clicked-item :"+item);
}
});
builder.setView(checkBoxView);
AlertDialog alert = builder.create();
alert.show();
return false;
}
});
在这方面的任何帮助将不胜感激。