gpt4 book ai didi

android-studio - 分类器没有伴随对象

转载 作者:行者123 更新时间:2023-12-03 16:54:23 24 4
gpt4 key购买 nike

我想在我的应用程序中使用 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()
}

}

关于android-studio - 分类器没有伴随对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51831537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com