Run time error in recyclerviewdemo.ContactsFragment.onCreateView

31 views Asked by At

I am writing a code for fetching a contact in one fragment and another one fragment has contacts and email id but in contacts fragment, there is a runtime error I cant rectify it am a student and learner only so check it out,

 recyclerView = view!!.findViewById<RecyclerView>(R.id.demo_recyclerview).apply{

            setHasFixedSize(true)


            layoutManager = viewManager


            adapter =demoadapter

        }

enter image description here

1

There are 1 answers

0
Birju Vachhani On

You're writing your code in onCreateView before inflating it first. That's why accessing view gives error. Move your code to onViewCreated method and don't use !!, it throws error if the object is null. Use ? instead.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    recyclerView = view.findViewById<RecyclerView>(R.id.demo_recyclerview).apply {
        setHasFixedSize(true)
        layoutManager = viewManager
        adapter = demoadapter
    }
}