Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 嵌套警报对话框_Java_Android - Fatal编程技术网

Java 嵌套警报对话框

Java 嵌套警报对话框,java,android,Java,Android,使用下面的代码,嵌套的AlertDialog遇到了一个无法解决的(对我来说)问题 final AlertDialog.Builder button_cook_action = new AlertDialog.Builder(this); final EditText cookMl = new EditText(this); cookMl.setInputType(InputType.TYPE_CLASS_NUMBER); button_cook.setOnClic

使用下面的代码,嵌套的AlertDialog遇到了一个无法解决的(对我来说)问题

    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();
        }
    });
第一次打电话很好,但当我第二次打电话时,它给了我:

FATAL EXCEPTION: main
    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我已经在这个论坛上搜索了,但是没有任何成功


如果有人有线索。提前感谢:)

您可以这样做-问题是,如果您第二次使用
EditText
时,它已经有一个父级-您需要在
onClick()中每次创建一个新的父级:


问题出现在alertDialog的setView中。每次创建对话框时,都必须对布局进行充气。在您的情况下,您正在膨胀EditText。因此,您可以在按钮中创建EditText\u cook onClickListener,或者采用@ligi发布的解决方案。

谢谢您的正确快速回答和解释!作品fine@mako那么你可能想考虑接受这个答案;感谢您对第二个解决方案的详细解释。很高兴能帮助您:)
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();
    }
});