Ryan Moeller
Ryan Moeller

Reputation: 151

Setting up RecyclerView in Fragment

I'm attempting to add a RecyclerView to a Fragment, but I'm running into an issue.

My code currently throws "java.lang.IllegalStateException: this must not be null" on line 44 of my code, which is:

layoutManager = LinearLayoutManager(this.context)

How do I properly reference my RecyclerView, so that I can initialize it with data?

package com.example.subshop

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_receipt.*
import kotlinx.android.synthetic.main.fragment_receipt.view.*
import kotlinx.android.synthetic.main.fragment_receipt.view.toppingRecyclerView

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class ReceiptFragment : Fragment() {

    private lateinit var toppingAdapter: ToppingAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        initRecyclerView()
        addToppingList()
    }

    private fun initRecyclerView() {

        toppingAdapter = ToppingAdapter()

        toppingRecyclerView.apply {
            layoutManager = LinearLayoutManager(this.context)
            adapter = toppingAdapter
        }
    }

    private fun addToppingList() {
        toppingAdapter.submitList(ReceiptFragmentArgs.fromBundle(arguments!!).sandwich.toppings)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_receipt, container, false)
        val sandwich = ReceiptFragmentArgs.fromBundle(arguments!!).sandwich

        view.sandwichAndBreadText.text = "${sandwich.name} on ${sandwich.breadName}"
        view.totalText.text = "Total: \$${sandwich.basePrice + sandwich.toppingPrice}"

        view.doneButton.setOnClickListener {
            findNavController().navigate(ReceiptFragmentDirections.actionReceiptFragmentToWelcomeFragment())
        }

        return view
    }
}

Upvotes: 2

Views: 2037

Answers (4)

Rakesh kushwaha
Rakesh kushwaha

Reputation: 759

Instead of

layoutManager = LinearLayoutManager(this.context)

Use

layoutManager = LinearLayoutManager(requireContext)

It will work

Also you can use like :

layoutManager = LinearLayoutManager(requireContext(), RecyclerView.VERTICAL, false)

Upvotes: 0

Jakir Hossain
Jakir Hossain

Reputation: 3930

Use activity instead of this.context like the following

  toppingRecyclerView.apply {
         layoutManager = LinearLayoutManager(activity)
         adapter = toppingAdapter
  }

And call initRecyclerView() from inside onViewCreated instead of onCreate like the following

// populate the views now that the layout has been inflated
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     super.onViewCreated(view, savedInstanceState)

     initRecyclerView()
     addToppingList()
}

Upvotes: 0

P. Leibner
P. Leibner

Reputation: 463

This is how i setup my recycleView in my fragments

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        viewManager = LinearLayoutManager(activity)
        viewAdapter = MyAdapter(requireContext())

        return inflater.inflate(R.layout.my_fragment, container, false)
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // Setup recycler view
        recyclerView = my_recycler_view.apply {
            setHasFixedSize(true)

            layoutManager = viewManager

            // specify an viewAdapter (see also next example)
            adapter = viewAdapter
        }

Upvotes: 0

Yavor Mitev
Yavor Mitev

Reputation: 1508

Probably the Activity, which is the context, is not attached. Try to do it in

override fun onActivityCreated(savedInstanceState: Bundle?) {}

or

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {}

Also don't use non-null asserts like arguments!!

try using:

        requireView()
        requireActivity()
        requireArguments()
        requireContext()

Upvotes: 1

Related Questions