Android Development custom dialog two times open error problem solved

Source: Internet
Author: User

before customizing a alertdialog dialog box, the first click is normal, but the second call will cause an error: Java.lang.IllegalStateException:The specified child already have a P Arent. You must call Removeview () on the child ' s parent first.

About this mistake tangled me for a long time, in the online Baidu also a lot, but feel the solution effect can not reach their desired effect. The explanation on the web is that a child view specifies multiple parent views. It can be inferred that when the second click button pops up the dialog, the child view is the same object as the child view of the first click, and the parent view is no longer the same object. But the feeling said foggy, after I study, found that I think the very good solution, want to share to everyone to communicate.

The code looks like this:

Builder = new Alertdialog.builder (mainactivity.this); Builder.setview (null); Builder.setview (Dialogview); Builder.settitle ("bookkeeping"); Builder.setpositivebutton ("OK", new Onclicklistener () {@ overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method Stubmysqliteopenhelper Help ER = new Mysqliteopenhelper (mainactivity.this); Sqlitedatabase database = Helper.getwritabledatabase (); Contentvalues values = new Contentvalues (), Values.put ("Sum", Edittext.gettext (). toString ()), Values.put ("project", Spinner.getitematposition ((int) spinner.getselecteditemid ()). ToString ()); if (checkbox.ischecked ()) {Values.put (" Category "," income "); if (checkbox2.ischecked ()) {Values.put ("category", "expense"); TextView TextView = (TextView) View.findviewbyid (r.id.textview1); Values.put ("Date", Textview.gettext (). toString ());                        Database.update ("Budget", Values, "date=", new string[] {textview.gettext (). toString ()});d Ialogview=null; if (dialogview==null) {DIalogview = Getlayoutinflater (). Inflate (r.layout.add_dialog, NULL);                    EditText = (editText) Dialogview.findviewbyid (R.ID.EDITTEXT1);                    Spinner = (spinner) Dialogview.findviewbyid (R.id.spinner1);                    checkbox = (checkbox) Dialogview.findviewbyid (R.id.checkbox1);                    CheckBox2 = (CheckBox) Dialogview.findviewbyid (R.ID.CHECKBOX2);  Clock = (DigitalClock) Dialogview.findviewbyid (R.ID.DIGITALCLOCK2);    Flag=true; }loadermanager.getloader (1001). oncontentchanged ();//If the state changes to trigger this method}}); Builder.setnegativebutton ("Cancel", n EW Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method stubd Ialogview=null;if (dialogview==null) {Dialogview = Getlayoutinflater (). Inflate (R.layout.add_dialog,                        NULL);                    EditText = (editText) Dialogview.findviewbyid (R.ID.EDITTEXT1); Spinner = (spinner) diAlogview.findviewbyid (R.id.spinner1);                    checkbox = (checkbox) Dialogview.findviewbyid (R.id.checkbox1);                    CheckBox2 = (CheckBox) Dialogview.findviewbyid (R.ID.CHECKBOX2);  Clock = (DigitalClock) Dialogview.findviewbyid (R.ID.DIGITALCLOCK2);    Flag=true;               }}); Builder.create (). Show (); }});

I've customized a dialog that contains a CheckBox control and a spinner control, and I want to dynamically spinner the content based on the tick of the checkbox space, so I've defined it as a global variable, which is what I said above.

The user suggested that the custom view should not be set as a global variable, should be set to local variables, Alert.builder created once, view through Getlayoutinflater once will not have two clicks on the dialog box, but this affects my program's effect.

According to my own continuous exploration, found the cause of the error, because my view is set as a global variable, the first time you create a dialog box, has been bound to a builder (that is, the last time the parent object), so in the second click on the dialog box, When you bind builder again, the new builder will not receive our custom view because it thinks you are already bound, and even if you destroy the previous builder in the dialog box, it is useless, because our view will still have a bound record of the past.

So how to solve it, my solution is that each time the dialog box to complete the task disappears, the view is set to NULL, re-dynamically get the layout, in fact, in order to achieve similar effects of local variables, but note that the custom layout of the control to be Findviewbyid again, Otherwise you will not be able to affect the control when you click the dialog box for the second time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.