AlialDialog nested warning

I faced undecidability issue (for me) with nested AlertDialog using the following code

    final AlertDialog.Builder button_cook_action = new AlertDialog.Builder(this);
    final EditText cookMl = new EditText(this);
    cookMl.setInputType(InputType.TYPE_CLASS_NUMBER);
    button_cook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            button_cook_action.setTitle(R.string.kitchen_recipe_button_cook)
                    .setMessage(R.string.kitchen_recipe_button_cook_volume)
                    .setView(cookMl)
                    .setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            AlertDialog.Builder builderCooking = new AlertDialog.Builder(RecipeActivity.this);
                            builderCooking.setTitle(recipe.getName())
                            .setMessage("message");
                            builderCooking.show();
                        }
                    })
                    .setNegativeButton(R.string.No, null)
                    .show();
        }
    });

      

The first call works fine, but when I call it the second time it gave me:

FATAL EXCEPTION: main
    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child parent first.

      

I already search in this forum but without any success.

If anyone has a key. Thanks in advance:)

+3


source to share


2 answers


You can do it like this: the problem was before, if you use EditText

a second time when it already has a parent - you need to create a new one every time every time inside yours onClick()

:



button_cook.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        final AlertDialog.Builder button_cook_action = new AlertDialog.Builder(this);
        final EditText cookMl = new EditText(this);
        cookMl.setInputType(InputType.TYPE_CLASS_NUMBER);

        button_cook_action.setTitle(R.string.kitchen_recipe_button_cook)
                .setMessage(R.string.kitchen_recipe_button_cook_volume)
                .setView(cookMl)
                .setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        AlertDialog.Builder builderCooking = new AlertDialog.Builder(RecipeActivity.this);
                        builderCooking.setTitle(recipe.getName())
                        .setMessage("message");
                        builderCooking.show();
                    }
                })
                .setNegativeButton(R.string.No, null)
                .show();
    }
});

      

+2


source


The problem is with the setView of your alertDialog. You have to inflate the layout every time you create your dialog. In your case, you are inflating the EditText. So either you have to create your EditText inside button_cook onClickListener, or accept the solution posted by @ligi.



+1


source







All Articles