About the exception "The specified child already has a parent. You

Source: Internet
Author: User

In the Android development process, sometimes you encounter the same problem in different situations:

[Java] view plaincopy

    1. 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

    1. 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

  1. Layoutinflater Inflater = (layoutinflater) getsystemservice (Layout_inflater_service);

  2. LinearLayout parent = (linearlayout) inflater.inflate (R.layout.main, null);

  3. ImageView child = (ImageView) Parent.findviewbyid (r.id.child);

  4. 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

    1. 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

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.