Android-the specified child already have a parent. You must call Removeview () on the child ' s parent first.

Source: Internet
Author: User

I've been having this problem for a long time, and there's a lot of people on the internet who say you need to find the control's parent, let the parent remove the control that needs to be added, and then add, for example:

if (view = null) {    ViewGroup parent = (ViewGroup) view.getparent ();    if (parent! = null) {        parent.removeview (view);}    } try {    view = inflater.inflate (R.layout.fragment_main, container, false);} catch (Inflateexception e) {}

Also many people said to change Inflater.inflate (r.layout.change_layout) to Inflater.inflate (R.layout.change_layout, container, false);

But my case is this, there is a Fragmentmanager, which controls 4 fragment, of which 3 fragment are directly fetched from the layout file, so call Inflater.inflate directly (R.layout.change_ Layout, container, false); Yes, but another fragment is dynamically loaded from my custom view class, and I always use AddView in the beginning, no matter how I go through the parent class to remove, the same error.

It was later found that in fragment's Oncreateview method, the AddView method was not displayed, as long as a view object was instantiated, and not even returned to the View object. As

@Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {  Framelayout = (framelayout) getactivity (). Findviewbyid (r.id.content); final String title = "Recent Weekly AQI Change chart";d ATA = new int[] { 1, 2, 3, 4, 5, 6, 7};xlables = new string[] {"One", "two", "three", "four", "five", "six", "seven"};view_brokenline = new Brokenlineview (f Ramelayout.getcontext (), title,xlables, NULL, data); return null;}

Android-the specified child already have a parent. You must call Removeview () on the child ' s parent first.

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.