supportFragmentManager.commit在Kotlin中不起作用。

huangapple go评论78阅读模式
英文:

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

supportFragmentManager.commit在Kotlin中不起作用。

答案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

huangapple
  • 本文由 发表于 2020年9月26日 19:40:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64077250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定