简体   繁体   中英

AlertDialog after AlertDialog

I am keep getting an error on my Code. I am trying to call a new AlertDialog inside of an AlertDialog. It seems to have a Problem showing salert, but i can't see why...

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();
}

The Error i am getting is:

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)

Any help or information would be helpfull.

You are using one view for multiple dialogs, one view can just have one parent.

Change:

final EditText input = new EditText(this);

salert.setView(input); // edit text added to alert
alert.setView(input); // edit text added to alert

To:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM