gpt4 book ai didi

android - Jetpack Compose - BottomSheetDialogFragment 和 ViewTreeLifecycleOwner 的问题

转载 作者:行者123 更新时间:2023-12-05 00:13:16 25 4
gpt4 key购买 nike

将 Jetpack Compose 库更新为 beta01 后,我无法显示 DialogFragment 或 BottomSheetDialogFragment。看起来 ViewTreeLifecycleOwner 无法再在我的 Activity 中找到。另外,我尝试了几种可能的 solutions但没有任何成功。

fragment 中的 View 被膨胀了:

 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(
R.layout.fragment_full_screen_layout, container, false
).apply {
findViewById<ComposeView>(R.id.compose_view).setContent {...}

对话框 fragment 显示为:

DialogFragmentExample.newInstance().show(supportFragmentManager, null)

有没有人遇到过类似的问题?欢迎提出任何建议。

堆栈跟踪:

java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from DecorView@409dd5d[MainActivity]
at androidx.compose.ui.platform.WindowRecomposer_androidKt.createLifecycleAwareViewTreeRecomposer(WindowRecomposer.android.kt:214)
at androidx.compose.ui.platform.WindowRecomposer_androidKt.access$createLifecycleAwareViewTreeRecomposer(WindowRecomposer.android.kt:1)
at androidx.compose.ui.platform.WindowRecomposerFactory$Companion$LifecycleAware$1.createRecomposer(WindowRecomposer.android.kt:98)
at androidx.compose.ui.platform.WindowRecomposerPolicy.createAndInstallWindowRecomposer$ui_release(WindowRecomposer.android.kt:151)
at androidx.compose.ui.platform.WindowRecomposer_androidKt.getWindowRecomposer(WindowRecomposer.android.kt:199)
at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:177)
at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.android.kt:222)

最佳答案

我什至没有使用 DialogFragment 也得到了同样的异常。

我的解决方法是从 Activity 设置 ViewTreeLifecycleOwner:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

ViewTreeLifecycleOwner.set(window.decorView, this)
// setContent(...); add fragment using Compose view
}
}

我目前正在使用以下库:

  • androidx.activity:activity-ktx:1.3.0-alpha04
  • androidx.fragment:fragment-ktx:1.3.1
  • androidx.compose.*:*:1.0.0-beta02

关于android - Jetpack Compose - BottomSheetDialogFragment 和 ViewTreeLifecycleOwner 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66587393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com