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.