Error:
The specified child already has a parent. You must call Removeview (). Answer:This error is straightforward, you Viewgroup.addview (Childview), Childview already has a Father view. The reason for the error is many, I mainly talk about Mlayoutinflater.inflate (ID, Rootview, false); This error is caused. (There are two kinds of this method, one is 2 parameters and one is 3 parameters).
2 parameters:First parameter: The XML file ID of layout; The second parameter: the root view in the XML file, which is the parent viewto which the child view is added. that is, the parent view layoutA: if Rootview is not NULL, the method return value is Rootview (Rootview has added a child view of the XML representation); b: If Rootview is empty, the method returns directly child view ( xml the child view of the representative); , ( So use mlayoutinflater.inflate (ID, rootview); The return is actually Rootview, And Rootview must have a parent layout (Decorateview, System-generated). So Rootview.addview (mlayoutinflater.inflate (ID, Rootview)). is equivalent to Rootview.addview (Rootview). So it's wrong.
3 parameters:the question is, how to fix the B case, preserving both the Layoutparams attribute in the XML file and the way to return to the parent view. two steps Rootview! = NULL (is the object arbitrarily), the third parameter = = false; mlayoutinflater.inflate (ID, Rootview, false), so that you can attach root view and keep layoutparams.
Android Layoutinflate.inflate method, Removeview () Error resolution