简体   繁体   中英

Alert Dialog error in Android

I have a problem with the alert dialog, I have 2 alert dialog and one should be showed after the user click on ok on the other one, the problem is when I try to show the second one (even if I use a handler to show it 12 second after the first one it still doesn't work) it crash, both works if they are alone but not together.

When it crashes it launches the class file editor and when I continue the emulation and then crash with the error, I think that is maded because the first one dialog is still there (even if the user click on a option) but I don't see any function to dismiss the dialog or something like it.

This is the code:

    final Builder dialogo = new AlertDialog.Builder(this)
                 .setTitle("Introduce un nombre")
                 .setMessage("Esta es la primera vez que subes una foto, introduce un nombre que aparecera en tu foto")
                 .setView(input)
                 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int whichButton) {
                         String nombre = String.valueOf(input.getText());
                        if(nombre.equalsIgnoreCase(""))
                        {
                            Toast.makeText(ctx,"¡Tu nombre no puede estar en blanco!", Toast.LENGTH_LONG).show();
                            //entra =1;
                        }
                        else
                        {                               
                            editor.putString("nombre",nombre);
                            editor.commit();
                            //entra = 0;                                 
                        }

                     }
                 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int whichButton) {
                     //entra =1;
                     }
                 });
                  dialogo.create();



                  Builder dialogo3 = new AlertDialog.Builder(this)
                     .setTitle("Introduce un nombre mamahuevo")
                     .setMessage("Esta es la primera vez que subes una foto, introduce un nombre que aparecera en tu foto")
                     .setView(input)
                     .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int whichButton) {
                             String nombre = String.valueOf(input.getText());
                            if(nombre.equalsIgnoreCase(""))
                            {
                                Toast.makeText(ctx,"¡Tu nombre no puede estar en blanco!", Toast.LENGTH_LONG).show();
                                //entra =1;
                            }
                            else
                            {                               
                                editor.putString("nombre",nombre);
                                editor.commit();

                                dialogo.show();
                                //entra = 0;                                 
                            }

                         }
                     }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int whichButton) {
                         //entra =1;
                         }
                     });
                      dialogo3.create();
                      dialogo3.show();

this is the error:

    06-06 15:43:15.765: E/AndroidRuntime(3008): FATAL EXCEPTION: main
    06-06 15:43:15.765: E/AndroidRuntime(3008): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3430)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at android.view.ViewGroup.addView(ViewGroup.java:3301)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at android.view.ViewGroup.addView(ViewGroup.java:3277)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at com.android.internal.app.AlertController.setupView(AlertController.java:416)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at com.android.internal.app.AlertController.installContent(AlertController.java:244)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at android.app.AlertDialog.onCreate(AlertDialog.java:337)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at android.app.Dialog.dispatchOnCreate(Dialog.java:355)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at android.app.Dialog.show(Dialog.java:260)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at sxa.sexolia.sexyono.Opciones$6.onClick(Opciones.java:183)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:169)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at android.os.Handler.dispatchMessage(Handler.java:99)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at android.os.Looper.loop(Looper.java:153)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at android.app.ActivityThread.main(ActivityThread.java:5297)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at java.lang.reflect.Method.invoke(Method.java:511)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    06-06 15:43:15.765: E/AndroidRuntime(3008):     at dalvik.system.NativeStart.main(Native Method)

问题解决了问题是.SetView(input)当我尝试将其从两个对话框中删除后尝试放入两个对话框时,它崩溃了,谢谢大家

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