我的代码不断出现错误。我正在尝试在AlertDialog内部调用新的AlertDialog。
似乎显示出salert有问题,但我不明白为什么...
public String passA = "";
public String passB = "";
public void createPassword() {
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final AlertDialog.Builder salert = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD
| InputType.TYPE_CLASS_TEXT);
salert.setView(input); // edit text added to alert
alert.setView(input); // edit text added to alert
salert.setTitle("Widerholen Sie Ihre PIN"); // title setted
alert.setTitle("Geben Sie eine PIN an"); // title setted
final OnClickListener b = new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
passB = input.getText().toString();
if (passA.equals(passB)) {
editor = settings.edit();
editor.putString("password", passA);
editor.commit();
logged_in = true;
return;
}
}
};
salert.setPositiveButton("OK", b);
OnClickListener a = new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
passA = input.getText().toString();
salert.show();
return;
}
};
alert.setPositiveButton("OK", a);
alert.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Functions.writeError("Falsche PIN Eingabe.");
finish();
}
});
alert.show();
}
我得到的错误是:
10-24 12:02:32.836: E/AndroidRuntime(2446): FATAL EXCEPTION: main
10-24 12:02:32.836: E/AndroidRuntime(2446): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
10-24 12:02:32.836: E/AndroidRuntime(2446): at android.view.ViewGroup.addViewInner(ViewGroup.java:3337)
10-24 12:02:32.836: E/AndroidRuntime(2446): at android.view.ViewGroup.addView(ViewGroup.java:3208)
10-24 12:02:32.836: E/AndroidRuntime(2446): at android.view.ViewGroup.addView(ViewGroup.java:3188)
10-24 12:02:32.836: E/AndroidRuntime(2446): at com.android.internal.app.AlertController.setupView(AlertController.java:401)
10-24 12:02:32.836: E/AndroidRuntime(2446): at com.android.internal.app.AlertController.installContent(AlertController.java:241)
10-24 12:02:32.836: E/AndroidRuntime(2446): at android.app.AlertDialog.onCreate(AlertDialog.java:336)
10-24 12:02:32.836: E/AndroidRuntime(2446): at android.app.Dialog.dispatchOnCreate(Dialog.java:353)
10-24 12:02:32.836: E/AndroidRuntime(2446): at android.app.Dialog.show(Dialog.java:257)
10-24 12:02:32.836: E/AndroidRuntime(2446): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
10-24 12:02:32.836: E/AndroidRuntime(2446): at de.pixelstein.nativ.NativeTestActivity$5.onClick(NativeTestActivity.java:257)
10-24 12:02:32.836: E/AndroidRuntime(2446): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
10-24 12:02:32.836: E/AndroidRuntime(2446): at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 12:02:32.836: E/AndroidRuntime(2446): at android.os.Looper.loop(Looper.java:137)
10-24 12:02:32.836: E/AndroidRuntime(2446): at android.app.ActivityThread.main(ActivityThread.java:4514)
10-24 12:02:32.836: E/AndroidRuntime(2446): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 12:02:32.836: E/AndroidRuntime(2446): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 12:02:32.836: E/AndroidRuntime(2446): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
10-24 12:02:32.836: E/AndroidRuntime(2446): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-24 12:02:32.836: E/AndroidRuntime(2446): at dalvik.system.NativeStart.main(Native Method)
任何帮助或信息将是有益的。
最佳答案
您将一个视图用于多个对话框,一个视图只能有一个父视图。
更改:
final EditText input = new EditText(this);
salert.setView(input); // edit text added to alert
alert.setView(input); // edit text added to alert
至:
final EditText firstInput = new EditText(this);
final EditText secondInput = new EditText(this);
salert.setView(firstInput); // edit text added to alert
alert.setView(secondInput); // edit text added to alert