Recently encountered a very troublesome problem, using Viewpager to dynamically add pages or delete pages when there is a problem (Java.lang.IllegalStateException:The specified child already have a Parent. You must call Removeview () on the child's parent first) and found a solution on StackOverflow. (http://stackoverflow.com/questions/22936886/java-lang-illegalstateexception-while-using-viewpager-in-android)
The original text is:
The problem is, in your adapters methodinstantiateItem
Callcontainer.addView(v);
But every View can has only one parent,
So it can is added only one time to a container via addView(...) .
When you open the popup the first time, everything works, becausev
Doesn ' t has a parent that time. But when you open your Popupwinow the second time,
It adds the view again to the container. That cerates the error.
Try to destroy the view if you close the popup view or remove all children views from it with
container.removeAllViews()
The workaround is to use the following method in Instantiateitem:
ViewGroup parent = (ViewGroup) v.getparent ();
if (parent! = NULL) {
Parent.removeallviews ();
}
Container.addview (v);
In the middle of many attempts have been approached the answer, but habitually to the v.getparent () forced into the View,view no Removeview () method, so that the way to abandon this method, later to think more, think of the solution if it is not their own desired results, Be sure to check again, it is possible that some small place did not notice.
Android illegalstateexception:the specified child already have a parent problem resolution