自定义对话框 - 您必须首先在孩子的父母上调用removeView()

时间:2014-09-03 13:16:46

标签: android android-dialog illegalstateexception customdialog

我为我的项目使用自定义AlertDialog,当我第二次尝试显示它时,它会告诉我java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

onCreate我的活动中,我有:

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

后来我用它作为AsyncTask的回复:

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

我第一次展示这个infoDialog它工作正常,但第二次它给了我IllegalStateException

我已经在StackOverflow上阅读了很多帖子,但似乎没有人能解决我的问题。 希望有人可以帮助我。

2 个答案:

答案 0 :(得分:1)

您可以使用下面的功能,然后在想要显示警报时调用此功能。

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

并像这样showDialog(loginResponse.getMessage())

调用此函数

答案 1 :(得分:0)

我有同样的问题,因为这个 我在创建alertlog之前设置了视图值

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

我在按钮内部设置了值视图,问题解决了。

其工作代码:

empezar.setOnClickListener(new View.OnClickListener(){             public void onClick(查看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();
        }
    });