In the Android development process, sometimes you encounter the same problem in different situations:
[Java] view plaincopy
Java.lang.IllegalStateException The specified child already have a parent. You must call Removeview () on the child ' s parent first.
which is the illegal state anomaly, which says this particular Childalready have aParentup, you must be in thisParentin the first callRemoveview ()method in order to continue with your content. It's obvious here that this Childis aView, a child ( Child)Viewmust depend on the parent (Parent)View, if you want to use this Child, you must passParent, and you want to use this if you are hard Child, then you have to let this Childwith theParentout of parent-child relationships (i.e.Removeview()) ... Forget or give a simple example to illustrate, province I said the mess, you listen to faint.
Create a new project, the layout file contains only one TextView and one ImageView, the layout is a linear layout (you can refer to the following source code), The result of the operation is to display a text and a picture,
In the onCreate () method of the Activity , we typically use the layout file main.xmlin the following way.
[Java] view plaincopy
Setcontentview (R.layout.main);
Here in order to explain the exception to be explained today, the use of a layout file, that is, the above line of code commented out, replaced by the following code:
Get Infalter Object
[Java] view plaincopy
Layoutinflater Inflater = (layoutinflater) getsystemservice (Layout_inflater_service);
LinearLayout parent = (linearlayout) inflater.inflate (R.layout.main, null);
ImageView child = (ImageView) Parent.findviewbyid (r.id.child);
Setcontentview (parent);
Layoutinflater Fill the layout of the class, do not understand can self-check, or have the opportunity I will be introduced in the blog, and then the file inflate linearlayout file, get child Span style= "font-family: the song Body;" , i.e. imageview setcontentview (parent) this layout main.xml show it, then get the effect and use only s Etcontentview (r.layout.main)
The following operation will be an exception, please note:
[Java] view plaincopy
Setcontentview (child);
That is, the parent in the above Setcontentview (parent) is replaced with child. See below for exceptions:
At this point in the exception it prompts to call Removeview ()in the parent again. Here we will obey the command, before Setcontentview (child), add a sentence parent.removeview (child), then no longer call Setcontentview (child) will not be abnormal, of course, the current display will be just a picture, and then if you call Setcontentview (parent) will only show the text content, as we have Child Remove drop it.
The code is as follows:
http://download.csdn.net/detail/yaolingrui/4129192
The
About exception "The specified child already has a parent. You