【发布时间】:2019-01-20 17:27:27
【问题描述】:
我想在我的应用程序中使用 BottomNavigationView,我在使用 kotlin 时遇到了这个问题(以前从未使用过 java)我看到了这条消息: 分类器“listFragment”没有伴随对象,因此必须在此处初始化
这是我的代码:
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()
}
感谢任何帮助:)
【问题讨论】:
-
我认为您没有正确初始化 listFragment,这就是您收到此错误的原因。能否请您添加此活动的完整代码和 LIstFragment 的代码?
-
我找到了解决方案,请查看答案@MehulKanzariya
标签: android-studio kotlin fragment companion-object