Skip to content
Advertisement

Why is my app Crashing with RecyclerView?

I’m doing a recycler method in my android studio project but i have a problem.

Everytime i’m trying to findViewById my application is crashing.

JavaScript

And i don’t understand why because i’m creating my view in the good way.

JavaScript

And i’m trying to continue with that

JavaScript

But i’m crashing at the line recyView = view.findViewById(R.id.recyclerBuy);. My R.id.recyclerBuy is not empty.

And here is my RecyclerView

JavaScript

Anyone have a clue why i’m crashing everytime ?

Advertisement

Answer

This happens because onAttach() callback is called before onCreateView().

The easiest way to fix the issue is placing the code where you finding your RecyclerView in onCreateView(). Something like:

JavaScript

A good article about fragments lifecycle: https://medium.com/androiddevelopers/the-android-lifecycle-cheat-sheet-part-iii-fragments-afc87d4f37fd

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement