classifier does not have a companion object
Asked Answered
L

2

7

i want to use BottomNavigationView in my app and i'm facing this problem with kotlin (never had it before with java) i see this message : classifier 'listFragment' does not have a companion object and thus must be initialized here

this is my code :

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
    when (item.itemId) {
        R.id.listNav -> {
//the problem is here in listFragment word below
            setFragment(listFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.accountNav -> {
//the problem is here also in accountFragment word below
            setFragment(accountFragment)
            return@OnNavigationItemSelectedListener true
        }
false 
}
private fun setFragment(fragment: Fragment) {
    supportFragmentManager.beginTransaction().replace(R.id.mainFrame , fragment).commit()
}

any help is appreciated :)

Liturgy answered 13/8, 2018 at 22:45 Comment(2)
I think you have not intialized the listFragment properly and thats why you are getting this error. Can you please add the full code of this activity and the code of the LIstFragment?Unfledged
i found the solution see the answer please @MehulKanzariyaLiturgy
L
5

i edited it this way and it worked :

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
    when (item.itemId) {
        R.id.listNav -> {
            val mFragment = cartFragment.newInstance()
            openFragment(mFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.cartNav -> {
            val mFragment = cartFragment.newInstance()
            openFragment(mFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.supportNav -> {
            val mFragment = supportFragment.newInstance()
            openFragment(mFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.accountNav -> {
            val mFragment = accountFragment.newInstance()
            openFragment(mFragment)
            return@OnNavigationItemSelectedListener true
        }
    }
    false
}
private fun openFragment(fragment: Fragment) {
    val transaction = supportFragmentManager.beginTransaction()
    transaction.replace(R.id.mainFrame, fragment)
    transaction.addToBackStack(null)
    transaction.commit()
}

fragments goes like this :

class listFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
        inflater.inflate(R.layout.fragment_list, container, false)

companion object {
    fun newInstance(): listFragment = listFragment()
}

}

Liturgy answered 14/8, 2018 at 14:22 Comment(0)
M
5

If someone is struggling (like I was) with the error in the title produced in a when block, when compared a sealed object, then don't forget the is keyword, like this:

when (someSealedClass) {
        is SomeSealedClass.Foo -> ...
//      ^^ don't forget this
      }
Macaco answered 9/12, 2020 at 5:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.