android - AlertDialog-The specified child already has a parent -
when trying add alertdialog.show()
in imageviews
(iview
, iview2
) , code crashes , giving
the specified child has parent. must call
removeview()
on child's parent first.
i dont know why happening. read error message , giving using 2 times alertdialog.show()
suggestion?
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(); } });
if comment alertdialog.show() it's working need it
looking @ of source, looks you're assigning same view instance (the edittext "input") alert dialog. each time show()
method called, dialog created via create()
. view supplied setview()
added dialog layout generated. since view added layout, has parent (the dialog layout). before can show dialog again, must remove view parent (the alertdialog), or instantiate new view.
simply moving line (and removing final modifier, doesn't need in scope) instantiates edittext
inside listener solved issue me. (i able reproduce)
iview.setonclicklistener(new view.onclicklistener() { @override public void onclick(final view v) { // create 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(); });
Comments
Post a Comment