"Java. lang. IllegalStateException: The specified child... & quot ;"

Source: Internet
Author: User

ViewPager is used for projects over the past few days, because it can achieve the effect of moving multiple pages left and right, and then

When ListView is used on each page, "PagerAdapter java. lang. IllegalStateException" is always displayed during runtime:

The specified child already has a parent. You must call removeView ()... "causes The program to end.

Displayed on PagerAdapter during debugging:

@ Override
Public Object instantiateItem (ViewGroup container, int position)
{
Container. addView (views. get (position ));
Return views. get (position );
}

The red code is incorrect. It is hard to understand that the newly added view will automatically bind a parent class. Because a son view cannot be related to two parent classes

You must unbind it.

A lot of solutions have been searched on the internet, mostly using mViewPager. setOffscreenPageLimit (views. size (); this method does not need to be determined

Whether the parent already exists, but the redundant listview still cannot be destroy.

I am going to post my solution to the problem. I hope that you don't have to worry about it:

@ Override
Public Object instantiateItem (ViewGroup container, int position)
{
Try {
If (views. get (position). getParent () = null ){
Container. addView (views. get (position ));
} Else {
(ViewGroup) views. get (position). getParent (). removeView (views. get (position ));
Container. addView (views. get (position ));
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return views. get (position );
}

Related Article

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.