Skip to content Skip to sidebar Skip to footer

How Can I Fix The Error 'java.lang.IllegalStateException: RecyclerView Must Not Be Null'

I have a recyclerView where I send data with my adapter but got the error RecyclerView must not be null code with my adpater: class InWaitingFragment : Fragment() { private

Solution 1:

You should change LinearLayoutManager(this) to LinearLayoutManager(this.context)


Solution 2:

For your LinearLayoutManager, Fragments aren't extending Context so you cant use this as parameters. Instead, use this:

waitingRecyclerView.layoutManager = LinearLayoutManager(context!!)

For the runtime error, "RecyclerView must not be null", it's because you're accessing the properties of the waitingRecyclerView inside the onCreateView callback. The layout hasn't been initialized yet. You can move your initialization of waitingRecyclerView to the 'onViewCreated' callback.

If you must initialize the waitingRecyclerView inside onCreateView you can access the waitingRecyclerView via the object you created when inflating the layout, i.e. inflate:

inflate.waitingRecyclerView.layoutManager = LinearLayoutManager(context!!)
inflate.waitingRecyclerView.adapter = adapter

Post a Comment for "How Can I Fix The Error 'java.lang.IllegalStateException: RecyclerView Must Not Be Null'"