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 ();
}