我有一个非组合视图,需要显示一个BottomSheetDialog。我想从根本上看:
myBottomSheetDialog = BottomSheetDialog(requireActivity(), R.style.Theme_Design_BottomSheetDialog)
val bottomSheetView = BottomsheetBinding.inflate(layoutInflater, null, false)
myBottomSheetDialog.setContentView(bottomSheetView.root)
其中BottomSheetBinding视图是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.compose.ui.platform.ComposeView
android:id="@+id/bottom_sheet_compose"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.compose.ui.platform.ComposeView>
</RelativeLayout>
当我打开对话框时,会引发以下异常。
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from android.widget.FrameLayout{ee8d547 V.E...... ......I. 0,0-0,0 #7f0a00c1 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:244)
at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:251)
at androidx.compose.ui.platform.AbstractComposeView.onMeasure(ComposeView.android.kt:288
出于几个原因,我不能让父视图首先组成。但是我想显示一个对话框,并使用compose作为该对话框中的内容。
发布于 2022-06-21 04:41:12
在调用setContentView
之前,必须首先声明组合策略:setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
发布于 2022-08-05 05:14:14
为了将可组合性用作任何窗口的内容,我们必须在视图树的根上设置视图树所有者。在大多数安卓主类中,它已经由google完成,但出于某种原因,他们忘记了BottomSheetDialog。
下面是解决方案,在调用setViewTreeOwners()之前调用BottomSheetDialog.setContentView()
private fun setViewTreeOwners() {
val activity = extractActivity(context) // if you have activity present nearby, then just use your activity here.
val decorView = window?.decorView
if (activity != null && decorView != null) {
ViewTreeLifecycleOwner.set(decorView, activity as? LifecycleOwner)
ViewTreeViewModelStoreOwner.set(decorView, activity as? ViewModelStoreOwner)
ViewTreeSavedStateRegistryOwner.set(decorView, activity as? SavedStateRegistryOwner)
}
}
从上下文中提取活动的可选代码
private fun extractActivity(context: Context?): Activity? {
var currentContext = context
while (currentContext != null) {
if (currentContext is Activity) return currentContext
if (currentContext !is ContextWrapper) break
currentContext = currentContext.baseContext
}
return null
}
https://stackoverflow.com/questions/72395779
复制相似问题