Android illegalstateexception:the specified child already have a parent problem resolution

Source: Internet
Author: User

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

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.