Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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()
    }
}

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading