0

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

GOVIND DIXIT
  • 1,748
  • 10
  • 27
  • can you paste the error log here? – GOVIND DIXIT Aug 04 '19 at 08:04
  • at com.example.recyclerviewdemo.ContactsFragment.onCreateView(Contacts.kt:31) – AKHILESH M Aug 04 '19 at 08:10
  • You are writing your code in onCreateView, and you still haven't inflated your view. You should move the code of onCreateView to onViewCreated, which is called by the system after the view has been inflated. – gmetal Aug 04 '19 at 08:28

1 Answers1

0

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
    }
}
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43