分类器没有伴随对象
classifier does not have a companion object
我想在我的应用程序中使用 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()
}
感谢任何帮助:)
我以这种方式编辑它并且它起作用了:
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()
}
片段是这样的:
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()
}
}
如果有人正在为 when
块中产生的标题错误而苦苦挣扎(就像我一样),
当比较 sealed
object 时,不要忘记 is
关键字,像这样:
when (someSealedClass) {
is SomeSealedClass.Foo -> ...
// ^^ don't forget this
}
我想在我的应用程序中使用 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()
}
感谢任何帮助:)
我以这种方式编辑它并且它起作用了:
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()
}
片段是这样的:
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()
}
}
如果有人正在为 when
块中产生的标题错误而苦苦挣扎(就像我一样),
当比较 sealed
object 时,不要忘记 is
关键字,像这样:
when (someSealedClass) {
is SomeSealedClass.Foo -> ...
// ^^ don't forget this
}