Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

ViewHolder views must not be attached when created? #87

Open
1ok1 opened this issue Feb 27, 2018 · 26 comments
Open

ViewHolder views must not be attached when created? #87

1ok1 opened this issue Feb 27, 2018 · 26 comments

Comments

@1ok1
Copy link

1ok1 commented Feb 27, 2018

02-28 05:16:37.431 1676-1676/com.elite.foodrepublic.dev E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.elite.foodrepublic.dev, PID: 1676
java.lang.IllegalStateException: ViewHolder views must not be attached when created. Ensure that you are not passing 'true' to the attachToRoot parameter of LayoutInflater.inflate(..., boolean attachToRoot)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6687)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at org.zakariya.stickyheaders.StickyHeaderLayoutManager.onLayoutChildren(StickyHeaderLayoutManager.java:200)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1191)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:876)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:895)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1171)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:761)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2496)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2212)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
02-28 05:16:37.431 1676-1676/com.elite.foodrepublic.dev E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

@1ok1
Copy link
Author

1ok1 commented Feb 27, 2018

@Override
public ItemCategoryViewAdapter.ViewHolder onCreateItemViewHolder(ViewGroup parent, int itemType) {
    View view = LayoutInflater.from(context).inflate(R.layout.indiv_stall_detail_category, parent, false);
    return new ItemCategoryViewAdapter.ViewHolder(view);
}

@Override
public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int headerType) {
    View v = LayoutInflater.from(context).inflate(R.layout.cart_header, parent, false);
    return new HeaderViewHolder(v);
}

Code reference for the above error.

Thanks in advance

@Dona278
Copy link

Dona278 commented Feb 28, 2018

I had the same error after updating support libraries to version 27.1.0

@Dona278
Copy link

Dona278 commented Feb 28, 2018

@elitelokesh seems to be caused by onCreateGhostHeaderViewHolder in SectioningAdapter.java and related to this pull request.

The parent.addView() method used here attach the view to the parent, so the problem aren't onCreateItemViewHolder or onCreateHeaderViewHolder custom implementation.

I found this workaround: override onCreateGhostHeaderViewHolder method into custom adapter like the others

@Override
public GhostHeaderViewHolder onCreateGhostHeaderViewHolder(ViewGroup parent) {
    final View ghostView = new View(parent.getContext());
    ghostView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    return new GhostHeaderViewHolder(ghostView);
}

@chaochaox1990
Copy link

@Dona278 your answer really help me Thanks. My problem is also 27.1.0 not working on that .

@KennyGoers
Copy link

Is there a release pending?

@milanPansuriya
Copy link

I am facing the same issue is there any update on this issue?

@CeccoCQ
Copy link

CeccoCQ commented May 3, 2018

Same issue.

@milanPansuriya
Copy link

I had found the solution for this
plz update your code
`public GhostHeaderViewHolder onCreateGhostHeaderViewHolder(ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());

View v = inflater.inflate(R.layout.ghost_view, parent, false);
return new GhostHeaderViewHolder(v);
}`

Please add an empty layout to onCreateGhostHeaderViewHolder

@WeiLianYang
Copy link

@Dona278 Thanks very much! I've solved the problem.

@kshipraShelke
Copy link

same error

@rion18
Copy link

rion18 commented Jul 10, 2018

I tried @Dona278's solution but now my recyclerview is completely empty. Any ideas? Debugging execution, I noticed that after onCreateGhostHeaderViewHolder, no onCreateItemViewHolder is being called. I have a NestedScrollView, and inside is the RecyclerView.

@Vishal99784
Copy link

@Dona278 Thanks very much! I've solved the problem.

@Arxing
Copy link

Arxing commented Oct 1, 2018

@Dona278 Thanks!!! I solved the problem.

@Divala
Copy link

Divala commented Oct 16, 2018

@Dona278 thanks!!

kenjiuno added a commit to HiraokaHyperTools/StickyHeaders that referenced this issue Nov 21, 2018
Fix problem "ViewHolder views must not be attached when created" on androidx.
Applying patch from ShamylZakariya#87 (comment)
@sacdulanga
Copy link

@Dona278 Thank you!

@chandmohd
Copy link

@Dona278 hey Thank you , I have migrated my project into AndoridX so had a problem so i tried your solution and it perfectly worked !

@Dona278
Copy link

Dona278 commented Mar 13, 2019

After a year seeing that my workaround still helps makes me happy! 🎉

@TothSteven
Copy link

@Dona278 Thank you! You saved the day for me :)

@MuruganSKutty
Copy link

MuruganSKutty commented Apr 1, 2019

I tried @Dona278 solution now above issue cleared but recyclerview is empty(showing nothing). I'm using 'com.android.support:appcompat-v7:27.1.1' & 'org.zakariya.stickyheaders:stickyheaders:0.7.6'. Please update if any solution.

@TothSteven
Copy link

Hi @MuruganSKutty
I also use 'com.android.support:appcompat-v7:27.1.1' and 'org.zakariya.stickyheaders:stickyheaders:0.7.6' and for me works perfectly

@MuruganSKutty
Copy link

I found another cause of invisible recyclerview, i.e (Recylerview is present inside the NestedScrollView). Please any help @Dona278 , @TothSteven .

@TothSteven
Copy link

Hi @MuruganSKutty
I'm using it in a tabLayout with fragments and initialize the sectioningAdapter in the Fragments onCreate and poputale the items in the Fragments onResume, have not tested it with NestedScrollView

@ShamylZakariya
Copy link
Owner

Hey I want to apologize to everybody. My job has been very demanding, and I've had to ignore all this. I've fixed the bug, and am upgrading to androidx, newer gradle etc. I will push a release in the next few days.

@Crasheng
Copy link

Crasheng commented Jul 2, 2019

@Dona278 hey Thank you , I have migrated my project into AndoridX so had a problem so i tried your solution and it perfectly worked !

@Dona278 @chandmohd i have updated to AndroidX and in my customer Adapter the one extending the SectionAdapter i have override the same method and it still gives me the same problem

@NandagopalR
Copy link

NandagopalR commented Nov 22, 2019

@elitelokesh seems to be caused by onCreateGhostHeaderViewHolder in SectioningAdapter.java and related to this pull request.

The parent.addView() method used here attach the view to the parent, so the problem aren't onCreateItemViewHolder or onCreateHeaderViewHolder custom implementation.

I found this workaround: override onCreateGhostHeaderViewHolder method into custom adapter like the others

@Override
public GhostHeaderViewHolder onCreateGhostHeaderViewHolder(ViewGroup parent) {
    final View ghostView = new View(parent.getContext());
    ghostView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    return new GhostHeaderViewHolder(ghostView);
}

thanks man. it's really working

@divy9t
Copy link

divy9t commented Mar 16, 2020

@Dona278 thanks for the answer mate, it's really helpful.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests