【发布时间】:2021-01-12 13:33:03
【问题描述】:
谁能帮我解决这个问题?在我的片段内 supportFragmentManager.commit{} 不起作用。 Android Studio 没有意识到这一点,我不知道该怎么做。我在 kotlin 项目中工作
【问题讨论】:
-
试试
.beginTransaction().commit()
标签: java android kotlin fragment
谁能帮我解决这个问题?在我的片段内 supportFragmentManager.commit{} 不起作用。 Android Studio 没有意识到这一点,我不知道该怎么做。我在 kotlin 项目中工作
【问题讨论】:
.beginTransaction().commit()
标签: java android kotlin fragment
commit {} 方法(事务在 lambda 中)是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 版本
【讨论】: