英文:
supportFragmentManager.commit in kotlin is not working
问题
在我的片段中,supportFragmentManager.commit{}部分不起作用。Android Studio未能识别此部分,我不知道该怎么办。我正在使用Kotlin项目。
英文:
Can anyone help me with this please? Inside my fragment supportFragmentManager.commit{} is not working. Android Studio is not recognizing this I don't know what to do. I am working in kotlin project
答案1
得分: 20
那个带有事务 lambda 的 commit {}
方法是由 Fragment KTX 库提供的扩展函数。如果你还没有添加这个依赖到 build.gradle
中:
dependencies {
implementation "androidx.fragment:fragment-ktx:1.2.5"
}
然后 Android Studio 应该会自动提示修复 commit
调用的导入。如果没有自动提示(有时会有些棘手),可以将这些导入语句添加到你的代码中:
import androidx.fragment.app.commit
也许这些也会需要,我不确定它们是否必要:
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
你可能需要删除一些其他相匹配的导入,以便只使用 androidx
版本。
英文:
That commit {}
method (with the transaction in a lambda) is an extension function provided by the Fragment KTX library. If you haven't already, you need to add this dependency in build.gradle
:
dependencies {
implementation "androidx.fragment:fragment-ktx:1.2.5"
}
and then Android Studio should automatically offer to fix the commit
call by importing it. If it doesn't (it can be awkward sometimes), add this to your imports:
import androidx.fragment.app.commit
and maybe these too, I don't know if they'll be necessary
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
you might need to remove some other matching imports so you're just using the androidx
versions
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论