Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Android Fragment recyclerView and context issue

I would like to create an recyclerView in a fragment, but it shows an error " java.lang.IllegalStateException: recylerView_Main must not be null at com.gph.bottomnavigation.FragmentMe.onCreateView(FragmentMe.kt:28)"

  • Question 1) Please kindly help to solve this issue.
  • Question 2) I created an recyclerView only in a empty project without any fragment, it is working properly.

enter image description here

But the same code is no working in Fragment, it shows error so I change "recylerView_Main.layoutManager = LinearLayoutManager(this)" to "recylerView_Main.layoutManager = LinearLayoutManager(context)" It shows no error and I can run in simlulator, but when I click the navigation button of the Fragment, the app stops and show this error. Please kindly help to solve it. enter image description here

Here with the code for FragmentMe.kt:

class FragmentMe : Fragment() {

        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,


               savedInstanceState: Bundle?): View? {

        recylerView_Main.layoutManager = LinearLayoutManager(context)
        recylerView_Main.adapter = Mainadapter()

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_me, container, false)
    }

}

Here with the code of MainActivity.kt:

class MainActivity : AppCompatActivity() {

    val manager = supportFragmentManager

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                //message.setText(R.string.title_home)
                createFragmentQpon()
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_dashboard -> {
                //message.setText(R.string.title_dashboard)
                createFragmentMe()
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_notifications -> {
                //message.setText(R.string.title_notifications)
                createFragmentTools()
                return@OnNavigationItemSelectedListener true
            }

        }
        false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Action Bar
        val actionBar = supportActionBar
        actionBar!!.setDisplayShowHomeEnabled(true)
        actionBar.setBackgroundDrawable(ColorDrawable(Color.parseColor("#00FFFFFF")))
        actionBar.setIcon(R.drawable.ic_home_black_24dp)
        actionBar.setDisplayShowTitleEnabled(false)

        createFragmentQpon()
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
    }

    fun createFragmentQpon() {
        val transaction = manager.beginTransaction()
        val fragment = FragmentQpon()
        transaction.replace(R.id.fragmentholder,fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    fun createFragmentMe() {
        val transaction = manager.beginTransaction()
        val fragment = FragmentMe()
        transaction.replace(R.id.fragmentholder,fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    fun createFragmentTools() {
        val transaction = manager.beginTransaction()
        val fragment = FragmentTools()
        transaction.replace(R.id.fragmentholder,fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }



}

Here with the code of Mainadapter.kt:

class Mainadapter: RecyclerView.Adapter<CustomViewHolder>() {

    val videolist = listOf("aaa","bbbb","cccc")

    override fun getItemCount(): Int {
        return  3
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {

        val layoutInflater = LayoutInflater.from(parent?.context)
        val cellForRow = layoutInflater.inflate(R.layout.tutorial_layout, parent, false)
        return CustomViewHolder(cellForRow)

    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {

        var videoName = videolist.get(position)


        holder.itemView.title.text = videoName

    }


}

class CustomViewHolder(v: View): RecyclerView.ViewHolder(v) {
}
like image 397
GPH Avatar asked May 19 '18 16:05

GPH


1 Answers

Move this code

recylerView_Main.layoutManager = LinearLayoutManager(context)
recylerView_Main.adapter = Mainadapter()

from onCreateView to onActivityCreated

override onActivityCreated and place the above code.

There are two things incorrect in your code :

  1. You are trying to access recyclerView even before inflating the View.

  2. The context of a Fragment is null in onCreateView and is usable in between onAttach and onDetach

like image 120
coder3101 Avatar answered Nov 10 '22 18:11

coder3101