AdamHurwitz
AdamHurwitz

Reputation: 10384

getFragmentManager() not working in Kotlin

I'm trying to launch a DialogFragment that is contained within a library module locally in my project. The class which I'm calling it from is using Kotlin and I'm getting the following error on the function getFragmentManager(): None of the following functions can be called with the arguments supplied

  import com.github.adamshurwitz.materialsearchtoolbar.SearchDialogFragment
    ...

    private var searchDialogFragment: SearchDialogFragment? = null
    ...

    searchDialogFragment = SearchDialogFragment()
    searchDialogFragment.show(supportFragmentManager, null)

I have another project where I am calling this in Java and it works fine using: getSupportFragmentManager().

Solutions I have tried:

searchDialogFragment.show(getFragmentManager(), null)
searchDialogFragment.show(supportFragmentManager, null)
searchDialogFragment.show(supportFragmentManager as FragmentManager, null)
searchDialogFragment.show(supportFragmentManager.beginTransaction(), null)
searchDialogFragment.show(supportFragmentManager(), null)

Upvotes: 6

Views: 7428

Answers (2)

Ramin Ar
Ramin Ar

Reputation: 1331

Simply replace with the following code:

searchDialogFragment.show(fragmentManager!!.beginTransaction(), null)

Be sure if you already imported: import androidx.fragment.app.Fragment

Upvotes: 0

Jakob Ulbrich
Jakob Ulbrich

Reputation: 133

Since your searchDialogFragment variable is marked as nullable with the question mark in the declaration you need to use the safe call operator ?.. It only executes when searchDialogFragment is not null:

searchDialogFragment?.show(supportFragmentManager, null)

Or you could use the following to declare your variable as not null but still be able to initialize it later in your code:

private lateinit var searchDialogFragment: SearchDialogFragment

Upvotes: 3

Related Questions