3

I'm using a custom AlertDialog for my project and, when i try to show it the second time it tells me java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

In onCreate of my activity i have:

infoDialog = new QustomDialogBuilder(this);
infoDialog.setTitle("Attenzione");
infoDialog.setTitleColor(Constants.ANTINORI_LIGHT);
infoDialog.setDividerColor(Constants.ANTINORI_LIGHT);
infoDialog.setNeutralButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }
});

Later i use it as reply of an AsyncTask:

//DO STUFF
infoDialog.setMessage(loginResponse.getMessage());
infoDialog.show();

The first time i show this infoDialog it works fine, but the second time it gives me the IllegalStateException.

I've read a lot of oher post on StackOverflow, but no one seems to solve my problem. Hope someone can help me.

Luca
  • 823
  • 4
  • 11
  • 31

2 Answers2

1

you can use the function below and then call this function when you want to show alert.

private void showDialog(String message) {
    final Dialog dialog = new Dialog(CustomDialog.this);
    dialog.setContentView(R.layout.custom_alert);
    dialog.setTitle("Custom Dialog");
    TextView text = (TextView) dialog.findViewById(R.id.textDialog);
    text.setText(message);
    ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
    dialog.show();
    Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
    declineButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
}

and call this function like this showDialog(loginResponse.getMessage())

Mubeen Ali
  • 2,150
  • 2
  • 18
  • 26
  • Have a Look http://androidexample.com/Custom_Dialog_-_Android_Example/index.php?view=article_discription&aid=88&aaid=111 – Naveen Tamrakar Sep 03 '14 at 13:37
  • That's quite like I do... In that link he use a button, in my code a use the built-in dialog button. We do the same things. – Luca Sep 03 '14 at 13:46
  • @Luca I updated my answer according to custom dialog have a look. I hope it will help. – Mubeen Ali Sep 03 '14 at 13:49
  • Try use this customDialog, the one I use in my project: [QustomDialog](https://gist.github.com/6a535704c560c5dd9b5b.git) – Luca Sep 03 '14 at 13:53
  • please review my answer again I updated it according to custom dialog. It is according to that link that you sent. – Mubeen Ali Sep 03 '14 at 13:59
  • 1
    @Luca your problem is just because you are just calling `infoDialog.show();` but you should put that in a function just like i did and pass your message string anc call function each time your problem will be fixed – Mubeen Ali Sep 03 '14 at 14:06
  • I used something like your answer and it works really fine! Thanks! – Luca Sep 03 '14 at 14:24
0

I have the same problem, because of this i set the view value before alertlog is created


LayoutInflater inflater = Primera.this.getLayoutInflater();

    view = inflater.inflate(R.layout.dialog,null);

    empezar.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            final AlertDialog.Builder builder = new AlertDialog.Builder(Primera.this);
            builder.setTitle(getResources().getString(R.string.dialog_codigo));

            builder.setView(view);
            builder.setPositiveButton(getResources().getString(R.string.dialog_aceptar), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    codigo = (EditText) view.findViewById(R.id.codigo);
                    ContentValues values = new ContentValues();
                    String valor;
                    valor = codigo.getText().toString();
                    values.put(Database.CODIGO_NOMBRE, valor);
                    mDbHelper.getWritableDatabase().insert(Database.TABLA_CODIGO, null, values);

                    if(codigo.getText().toString() == null || codigo.getText().toString().equals("")){
                        Toast.makeText( getApplicationContext(), "Codigo incorrecto" , Toast.LENGTH_SHORT ).show();
                    }
                    else
                    {
                        Toast.makeText( getApplicationContext(), "Codigo correcto" , Toast.LENGTH_SHORT ).show();
                        Intent intent = new Intent(Primera.this, Producto.class);
                        intent.putExtra("opcion",0);
                        intent.putExtra("primera",1);
                        startActivity(intent);
                    }
                }
            });
            builder.setNegativeButton(getResources().getString(R.string.dialog_atras), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                    dialog.cancel();

                }
            });
            builder.create();
            builder.show();
        }
    });

I set value view inside of the button, and problem solved.

Its worked code:


empezar.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {

            final AlertDialog.Builder builder = new AlertDialog.Builder(Primera.this);
            builder.setTitle(getResources().getString(R.string.dialog_codigo));
            LayoutInflater inflater = Primera.this.getLayoutInflater();
            view = inflater.inflate(R.layout.dialog,null);
            builder.setView(view);
            builder.setPositiveButton(getResources().getString(R.string.dialog_aceptar), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    codigo = (EditText) view.findViewById(R.id.codigo);
                    ContentValues values = new ContentValues();
                    String valor;
                    valor = codigo.getText().toString();
                    values.put(Database.CODIGO_NOMBRE, valor);
                    mDbHelper.getWritableDatabase().insert(Database.TABLA_CODIGO, null, values);

                    if(codigo.getText().toString() == null || codigo.getText().toString().equals("")){
                        Toast.makeText( getApplicationContext(), "Codigo incorrecto" , Toast.LENGTH_SHORT ).show();
                    }
                    else
                    {
                        Toast.makeText( getApplicationContext(), "Codigo correcto" , Toast.LENGTH_SHORT ).show();
                        Intent intent = new Intent(Primera.this, Producto.class);
                        intent.putExtra("opcion",0);
                        intent.putExtra("primera",1);
                        startActivity(intent);
                    }
                }
            });
            builder.setNegativeButton(getResources().getString(R.string.dialog_atras), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                    dialog.cancel();

                }
            });
            builder.create();
            builder.show();
        }
    });