推断类型为 DashboardFragment.Companion 但 Fragment 应在函数 replaceFragment 中
inferred type is DashboardFragment.Companion but Fragment was expected in function replaceFragment
private val dashboardFragment = DashboardFragment
private val settingsFragment = SettingsFragment
private val infoFragment = InfoFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHomePageBinding.inflate(layoutInflater)
setContentView(binding?.root)
replaceFragment(dashboardFragment)
}
private fun replaceFragment(fragment: Fragment){
if(fragment != null){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.frag_container, fragment)
transaction.commit()
}
}
我的项目中设置了 3 个片段,即仪表板、设置和信息。每次我在函数 replaceFragment 中输入我的片段名称时,我都会收到类型不匹配的错误。我指的是这个视频 - https://www.youtube.com/watch?v=v8MbOjBCu0o&t=1s
您需要实例化这些字段。现在他们只是指向 class 类型。它类似于 java 中的 new
关键字。
private val dashboardFragment = DashboardFragment()
private val settingsFragment = SettingsFragment()
private val infoFragment = InfoFragment()
private val dashboardFragment = DashboardFragment
private val settingsFragment = SettingsFragment
private val infoFragment = InfoFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHomePageBinding.inflate(layoutInflater)
setContentView(binding?.root)
replaceFragment(dashboardFragment)
}
private fun replaceFragment(fragment: Fragment){
if(fragment != null){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.frag_container, fragment)
transaction.commit()
}
}
我的项目中设置了 3 个片段,即仪表板、设置和信息。每次我在函数 replaceFragment 中输入我的片段名称时,我都会收到类型不匹配的错误。我指的是这个视频 - https://www.youtube.com/watch?v=v8MbOjBCu0o&t=1s
您需要实例化这些字段。现在他们只是指向 class 类型。它类似于 java 中的 new
关键字。
private val dashboardFragment = DashboardFragment()
private val settingsFragment = SettingsFragment()
private val infoFragment = InfoFragment()