AlertDialog - 指定的子级已有父级

时间:2016-01-16 20:03:41

标签: android crash

当我尝试在alertDialog.show()ImageViewsiView)中添加iView2时,我的代码崩溃并提供

  

指定的子级已有父级。您必须先在孩子的父母身上致电removeView()

我不知道为什么会这样。实际上我读到了这个错误信息,并且可能会使用2次alertDialog.show()任何建议?

mLayout = (RelativeLayout) findViewById(R.id.relaLayout);
final EditText input = new EditText(MainActivity.this);
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT
            ,LinearLayout.LayoutParams.MATCH_PARENT);

的iView

input.setLayoutParams(lp);
    iView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            select = 0;

            alertDialog.setView(input);
            alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    m_Text = input.getText().toString();
                    mLayout.addView(cth[0].setCardView(select, m_Text));
                    iView.setClickable(false);
                }
            });
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            alertDialog.show();
        }
    });

iView2

iView2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            select = 1;
            //input.setLayoutParams(lp);
            alertDialog.setView(input);
            alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    m_Text = input.getText().toString();
                    mLayout.addView(cth[0].setCardView(select, m_Text));
                    iView2.setClickable(false);
                }
            });
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            alertDialog.show();
        }
    });

如果我评论alertDialog.show()然后它正在工作,但我需要它

1 个答案:

答案 0 :(得分:8)

查看一些来源,看起来您正在为警报对话框分配相同的视图实例(EditText"输入")。每次调用show()方法时,都会通过create()创建对话框。然后,提供给setView()的视图将添加到生成的对话框布局中。由于该视图已添加到布局中,因此它现在具有父级(对话框布局)。在再次显示对话框之前,必须从父级(AlertDialog)中删除视图,或者实例化新视图。

简单地移动行(并移除最终修饰符,它在此范围内并不需要它),将EditText实例化到侦听器内部为我解决了问题。 (我能够重现)

iView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {
        // Create the edit text view.
        EditText input = new EditText(MainActivity.this);

        // Set it.
        alertDialog.setView(input);

        // Set it.
        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                m_Text = input.getText().toString();
                mLayout.addView(cth[0].setCardView(select, m_Text));
                v.setClickable(false);
            }
        });

        // Set it.
        alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });

        // Show it.
        alertDialog.show();
});