Android custom Dialog second call error solution: The specified child already has

Source: Internet
Author: User

Today, a custom AlertDialog dialog box is created. It is normal when you click it for the first time, but an error occurs during the second call: java. lang. illegalStateException: The specified child already has a parent. you must call removeView () on the child's parent first.

 


The error code is:


[Java]
// Window Constructor
Synchronized protected View GetTextEntryView (){
LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
Switch (Dialog_TYPE ){
Case DIALOG_TYPE_EXIT: // exit dialog box
Return inflater. inflate (R. layout. datalist, null );
Default:
Break;
}
Return null;
}

// Window Constructor
Synchronized protected View GetTextEntryView (){
LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
Switch (Dialog_TYPE ){
Case DIALOG_TYPE_EXIT: // exit dialog box
Return inflater. inflate (R. layout. datalist, null );
Default:
Break;
}
Return null;
} The prompt indicates that multiple parent views are specified for a subview. It can be inferred that, when the second click button dialog box is displayed, the Child view and the Child view at the first click are the same object, and the parent view is no longer the same object.

 

 

The solution is very simple. You only need to override the show () method in the dialog box:


[Java]
@ Override // rewrite the show () method
Public AlertDialog show (){
// Bind the View again
SetView (GetTextEntryView ());
Return super. show ();
}

@ Override // rewrite the show () method
Public AlertDialog show (){
// Bind the View again
SetView (GetTextEntryView ());
Return super. show ();
}

Related Article

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.