RecyclerView: The specified child already has a parent. You must first call removeView () on the parent parent

I have a RecyclerView where I get the exception below. I just made an exception in Crashlytics, I never managed to reproduce the problem myself, despite the fact that she actively used this activity every day.

The way I inflate my view on onCreateViewHolder is:

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

There is another type of view that comes from MoPub's own ad.

Any idea what could be the problem?

 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) 

Edit: code

 @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)); } } 
+5
source share
1 answer

Setting in onCreateViewHolder how it worked for me

 View view = View.inflate(mContext,R.layout.layout_name,null); 

Instead

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

Where parent created a problem with setting this parent to null to solve the problem.

0
source

Source: https://habr.com/ru/post/1270898/


All Articles