AlertDialog-指定的子项已经有父级了。

3
当我试图在ImageViews(iViewiView2)中添加alertDialog.show()时,我的代码崩溃并显示以下错误信息:

指定的子项已经有一个父级。您必须首先对子项的父级调用removeView()

我不知道为什么会出现这种情况。实际上,我已经阅读了关于此错误消息的内容,并且可能是由于使用了两次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(),那么它就能正常工作,但是我需要它。
2个回答

10

看起来你正在将相同的视图实例(EditText “input”)分配给警报对话框。 每次调用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();
});

0

看一下你的代码,你正在调用 show() 方法到 AlertDialog.Builder 实例中。这就是为什么你会遇到这个问题。

所以你需要使用 alertDialogBuilder.create() 创建一个 AlertDialog 对象实例。这个 create() 方法返回 AlertDialog 对象,之后你可以调用 show() 方法。 因此,让我们来看一下以下代码片段,以更好地理解。 首先,你需要创建一个名为 createDailogObject 的方法。

createDialogObject()

public/private AlertDialog dialogName; //create global variable
...
createDialogObject();// this line create instance of AlertDialog
...
public void createDialogObject(){
       mLayout = (RelativeLayout) findViewById(R.id.relaLayout);
       EditText input = new EditText(MainActivity.this);
       AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
       LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            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();
                }
            });

           // this following line will creates the AlertDialog instance
           dialogName = .create();
}
...

现在你可以在任何地方调用show方法了!!!。

iView1

    iView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialogName.show();
        }
    });

还有...

iView2

    iView2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialogName.show();
        }
    });

我会百分之百肯定地工作


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接