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()
}
}
I have 3 fragments setup in my project namely - Dashboard, settings and Info. I am getting an error for type mismatch everytime I enter my fragment name in the function replaceFragment. I am referring to this video - https://www.youtube.com/watch?v=v8MbOjBCu0o&t=1s
CodePudding user response:
You need to instantiate these fields . right now they r just pointing to the class type . Its similar to new
keyword in java .
private val dashboardFragment = DashboardFragment()
private val settingsFragment = SettingsFragment()
private val infoFragment = InfoFragment()