首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在布局中使用ComposeView或使用BottomSheetDialog引发异常

在布局中使用ComposeView或使用BottomSheetDialog引发异常
EN

Stack Overflow用户
提问于 2022-05-26 17:28:08
回答 2查看 474关注 0票数 1

我有一个非组合视图,需要显示一个BottomSheetDialog。我想从根本上看:

代码语言:javascript
运行
复制
myBottomSheetDialog = BottomSheetDialog(requireActivity(), R.style.Theme_Design_BottomSheetDialog)
val bottomSheetView = BottomsheetBinding.inflate(layoutInflater, null, false)
myBottomSheetDialog.setContentView(bottomSheetView.root)

其中BottomSheetBinding视图是:

代码语言:javascript
运行
复制
<?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>

当我打开对话框时,会引发以下异常。

代码语言:javascript
运行
复制
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作为该对话框中的内容。

EN

回答 2

Stack Overflow用户

发布于 2022-06-21 04:41:12

在调用setContentView之前,必须首先声明组合策略:setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)

票数 0
EN

Stack Overflow用户

发布于 2022-08-05 05:14:14

为了将可组合性用作任何窗口的内容,我们必须在视图树的根上设置视图树所有者。在大多数安卓主类中,它已经由google完成,但出于某种原因,他们忘记了BottomSheetDialog。

下面是解决方案,在调用setViewTreeOwners()之前调用BottomSheetDialog.setContentView()

代码语言:javascript
运行
复制
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)
    }
}

从上下文中提取活动的可选代码

代码语言:javascript
运行
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72395779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档