'boolean android.support.v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference when doing recyclerView.addView

后端 未结 2 1977
难免孤独
难免孤独 2021-01-18 07:33

Why when I tried to add additional view to recyclerView after recyclerView.setAdapter()

return this error

Attempt to invoke virtual meth         


        
相关标签:
2条回答
  • 2021-01-18 07:52

    This error occur, because RecyclerView creates its own views (see .onCreateViewHolder() method) - it manages views itself, you cannot add view like that. RecyclerView (that's why it called so) reuses inflated views so it don't have to create new view every time. You should add some data object and bind that object in ViewHolder implementation

    0 讨论(0)
  • 2021-01-18 08:16

    I think, the problem is:

    You have set the adapter of recyclerview to the adapter that you have made with the class

    Adapters: An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

    So your adapter view has already provided the view to your recyclerView items.

    Now when you are trying to insert an item that is out of context of the view that adapter has provided, it shows an error that your view might not have the same context as adapter's.

    So it is not accepting textView in your recyclerView as it should be in the form of cardview that your adapter class is providing.

    0 讨论(0)
提交回复
热议问题