luckybilly / luckybilly/Gloading

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

Open
#36 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.4k
Forks
168
PR merge metrics
No merged PRs in 30d

Description

错误
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);
    }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the exception in the fragment and ViewPager setup shown in the issue, then inspect the ViewGroup call site and the wrap(View view) method. Trace the view’s parent while loading is shown and removed; done means the fragment can load and dismiss its loading view without the child-already-has-a-parent exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java
Domain
mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.