RecyclerView:指定的子级已有父级。您必须首先在孩子的父母上调用removeView()

时间:2017-08-15 19:24:26

标签: android android-recyclerview recycler-adapter android-viewholder

我有一个RecyclerView我在下面得到了例外。我只是在Crashlytics上获得了例外,尽管我每天都在大量使用这项活动,但我自己也没有设法重现这个问题。

我在onCreateViewHolder上浏览我的观点的方式是:

View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item, parent, false);

还有另一种视图类型,来自MoPub原生广告。

知道问题是什么吗?

Caused by java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the childs parent first.
           at android.view.ViewGroup.addViewInner(ViewGroup.java:4438)
           at android.view.ViewGroup.addView(ViewGroup.java:4274)
           at android.view.ViewGroup.addView(ViewGroup.java:4215)
           at android.support.v7.widget.RecyclerView$5.addView(RecyclerView.java:711)
           at android.support.v7.widget.ChildHelper.addView(ChildHelper.java:107)
           at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:7877)
           at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:7835)
           at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:7823)
           at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1565)
           at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
           at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1325)
           at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1061)
           at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4726)
           at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
           at android.view.Choreographer.doCallbacks(Choreographer.java:696)
           at android.view.Choreographer.doFrame(Choreographer.java:628)
           at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
           at android.os.Handler.handleCallback(Handler.java:815)
           at android.os.Handler.dispatchMessage(Handler.java:104)
           at android.os.Looper.loop(Looper.java:207)
           at android.app.ActivityThread.main(ActivityThread.java:5728)
           at java.lang.reflect.Method.invoke(Method.java)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:683)

编辑:代码

@Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == MoPubStreamAdPlacer.CONTENT_VIEW_TYPE) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item, parent, false);
            ViewHolder vh = new ViewHolder(this, v);
            return vh;
        } else {
            final MoPubAdRenderer adRenderer = streamAdPlacer.getAdRendererForViewType(viewType);
            if (adRenderer == null) {
                MoPubLog.w("No view binder was registered for ads in MoPubRecyclerAdapter.");
                // This will cause a null pointer exception.
                return null;
            }
            return new MoPubRecyclerViewHolder(
                    adRenderer.createAdView((Activity) parent.getContext(), parent));
        }


    }

0 个答案:

没有答案