gpt4 book ai didi

android - 包含 ComposeView 的 BottomSheetDialog

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

我正在尝试用 AppCompatActivity 中的撰写 View 填充底页对话框:

val bottomSheet = BottomSheetDialog(this)

bottomSheet.setContentView(ComposeView(this).apply {
setContent {
MaterialTheme {
Test()
}
}
})

bottomSheet.show()
使用 androidx.fragment:fragment-ktx:1.4.0-alpha10 , androidx.appcompat:appcompat:1.3.1得到以下异常:
    java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from android.widget.FrameLayout{386906d V.E...... ......I. 0,0-0,0 #7f0900d3 app:id/container}
at androidx.compose.ui.platform.WindowRecomposer_androidKt.createLifecycleAwareViewTreeRecomposer(WindowRecomposer.android.kt:244)
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:99)
at androidx.compose.ui.platform.WindowRecomposerPolicy.createAndInstallWindowRecomposer$ui_release(WindowRecomposer.android.kt:155)
at androidx.compose.ui.platform.WindowRecomposer_androidKt.getWindowRecomposer(WindowRecomposer.android.kt:230)
at androidx.compose.ui.platform.AbstractComposeView.resolveParentCompositionContext(ComposeView.android.kt:220)
at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:227)
at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.android.kt:259)
at android.view.View.dispatchAttachedToWindow(View.java:19553)
...

最佳答案

我最终继承了 BottomSheetDialogFragment并在那里使用撰写 View :

class MyBottomSheet : BottomSheetDialogFragment() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View = ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
// working
}
}
}
并从这样的 Activity 中调用它:
val modalBottomSheet = MyBottomSheet()
modalBottomSheet.show(supportFragmentManager, "SomeTAG")
以防万一有人遇到同样的问题。

关于android - 包含 ComposeView 的 BottomSheetDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69741797/

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