D4rWiNS
D4rWiNS

Reputation: 2615

Show 2 alert dialog in the same actvity

Hi i am trying to set 2 alert dialog, 1 after the other,but it crash and i dont know how to solve it, the first one its showed but when i click on "ok" it stop (it doesnt crash with the red error but it stop working)

if someone can figure out where is the problem it will save me there is the code

    AlertDialog 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;
                             AlertDialog dialogo3 = new AlertDialog.Builder(ctx)
                             .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("Hombre", new DialogInterface.OnClickListener() {
                                 public void onClick(DialogInterface dialog, int whichButton) 
                                 {

                                        editor.putString("genero","Hombre");
                                        editor.commit();


                                        GestorSubirFotos gestor = new GestorSubirFotos(ctx,prefs);
                                        String elnombre = prefs.getString("nombre", "");
                                        gestor.execute(getRealPathFromURI(selectedImage),id,elnombre,phoneid);

                                 }
                             }).setNegativeButton("Mujer", new DialogInterface.OnClickListener() {
                                 public void onClick(DialogInterface dialog, int whichButton) {

                                        editor.putString("genero","Mujer");
                                        editor.commit();


                                        GestorSubirFotos gestor = new GestorSubirFotos(ctx,prefs);
                                        String elnombre = prefs.getString("nombre", "");
                                        gestor.execute(getRealPathFromURI(selectedImage),id,elnombre,phoneid);
                                 }
                             }).show();
                        }

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

i also tryed that way, but it doesnt work either

     final Builder dialogo3 = new AlertDialog.Builder(ctx)
                 .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("Hombre", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int whichButton) 
                     {

                            editor.putString("genero","Hombre");
                            editor.commit();


                            GestorSubirFotos gestor = new GestorSubirFotos(ctx,prefs);
                            String elnombre = prefs.getString("nombre", "");
                            gestor.execute(getRealPathFromURI(selectedImage),id,elnombre,phoneid);

                     }
                 }).setNegativeButton("Mujer", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int whichButton) {

                            editor.putString("genero","Mujer");
                            editor.commit();


                            GestorSubirFotos gestor = new GestorSubirFotos(ctx,prefs);
                            String elnombre = prefs.getString("nombre", "");
                            gestor.execute(getRealPathFromURI(selectedImage),id,elnombre,phoneid);
                     }
                 });
                  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;

                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                public void run() {

                                    dialogo3.show();
                                }
                            }, 2000);

                        }

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

                  dialogo.show();

Upvotes: 3

Views: 926

Answers (2)

mrsus
mrsus

Reputation: 5486

try this, now it should work--->

 DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
                    //do something
        }
 };
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Do you really want to unjoin this event?")
                    .setPositiveButton("Yes", dialogClickListener)
                    .setNegativeButton("No", null)
                    .show();

Upvotes: 2

Mohamed_AbdAllah
Mohamed_AbdAllah

Reputation: 5322

You are missing calling .create() before calling .show() for the Builder to create the Dialog.

Upvotes: 1

Related Questions