Skip to content

fragment+viewpager,在fragment中使用,会报错: #36

Description

@funnyzhaov

错误
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

出错抛异常源码:
ViewGroup

  if (child.getParent() != null) {
            throw new IllegalStateException("The specified child already has a parent. " +
                    "You must call removeView() on the child's parent first.");
        }

使用场景

创建Fragment页面与viewpager关联

        private void createTypeFragment(List<SearchTypeBean> searchTypeBeanList){

        SearchTypeBean beanAll=new SearchTypeBean();
        beanAll.setAction(0);
        beanAll.setTitle(getResources().getString(R.string.search_all_btn));

        searchTypeBeanList.add(0,beanAll);
        mPagerAdapter.refreshFragments(searchTypeBeanList,mKeyword);
        mTypeVp.setOffscreenPageLimit(searchTypeBeanList.size()-1);
        mTypeVp.setAdapter(mPagerAdapter);
        mTypeTabLayout.setupWithViewPager(mTypeVp);

    }

Fragment加载数据,先展示loading,再关闭

推测原因

看代码应该是这段代码出的问题,求解

   public Holder wrap(View view) {
        FrameLayout wrapper = new FrameLayout(view.getContext());
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp != null) {
            wrapper.setLayoutParams(lp);
        }
        if (view.getParent() != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            int index = parent.indexOfChild(view);
            parent.removeView(view);
            parent.addView(wrapper, index);
        }
        LayoutParams newLp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        wrapper.addView(view, newLp);
        return new Holder(mAdapter, view.getContext(), wrapper);
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions