首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >androidx.constraintlayout.widget.ConstraintLayout中找不到java.lang.IllegalStateException: ViewTreeLifecycleOwner

androidx.constraintlayout.widget.ConstraintLayout中找不到java.lang.IllegalStateException: ViewTreeLifecycleOwner
EN

Stack Overflow用户
提问于 2021-07-15 14:44:38
回答 3查看 3.3K关注 0票数 8

当我尝试用XML插入Compose (在其他应用程序上)时,我得到了以下例外:

代码语言:javascript
运行
复制
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.constraintlayout.widget.ConstraintLayout{d596746 V.E...... ......ID 0,0-0,0}

但没有覆盖(在活动中),它的工作正常。有人知道怎么解决吗?我已经将AppCompat库更新为1.3.0

我的XML代码:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">
    <androidx.compose.ui.platform.ComposeView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/compose_view"/>
</androidx.constraintlayout.widget.ConstraintLayout>

我的覆盖代码:

代码语言:javascript
运行
复制
mParams = WindowManager.LayoutParams(
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
    PixelFormat.TRANSLUCENT
)
layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
mView = layoutInflater.inflate(R.layout.power_overlay, null)
mParams!!.gravity = Gravity.CENTER
mWindowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
mWindowManager.addView(mView, mParams)
EN

回答 3

Stack Overflow用户

发布于 2021-09-17 09:11:39

对我来说,这是因为我没有包含appcompat库,我的活动继承自activity,而不是AppCompatActivity。通过添加库解决了问题:

代码语言:javascript
运行
复制
implementation("androidx.appcompat:appcompat:1.3.1")

并继承自AppCompatActivity

代码语言:javascript
运行
复制
class MyActivity: AppCompatActivity() {
  ...
}
票数 3
EN

Stack Overflow用户

发布于 2022-05-11 10:06:27

对我来说,将androidx.appcompat:appcompat1.0.0升级到1.4.1,问题就解决了。

碎片:

代码语言:javascript
运行
复制
class Xxx : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    val view = inflater.inflate(R.layout.fragment_xxx, container, false)

    view.findViewById<ComposeView>(R.id.compose_view).apply {
        setViewCompositionStrategy(
            ViewCompositionStrategy.DisposeOnLifecycleDestroyed(viewLifecycleOwner)
        )
        setContent {
            // compose
        }
    }
    return view
}

xml:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <androidx.compose.ui.platform.ComposeView
        android:id="@+id/compose_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
票数 3
EN

Stack Overflow用户

发布于 2022-08-31 19:18:05

我写了这段代码,它起作用了。

代码语言:javascript
运行
复制
class AndroidComposeDialog<T>(
    private val activity: T,
    private val content: @Composable () -> Unit
) : Dialog(activity) where T : Context, T : androidx.lifecycle.LifecycleOwner, T : ViewModelStoreOwner, T : SavedStateRegistryOwner, T : OnBackPressedDispatcherOwner {


    private fun initViewTreeOwners() {
        val window = window ?: return
        ViewTreeLifecycleOwner.set(window.decorView, activity)
        ViewTreeViewModelStoreOwner.set(window.decorView, activity)
        window.decorView.setViewTreeSavedStateRegistryOwner(activity)
        window.decorView.setViewTreeOnBackPressedDispatcherOwner(activity)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //here i init view tree owner that will be used by compose view
        initViewTreeOwners()
        //here i hide background of dialog
        window?.setBackgroundDrawable(ColorDrawable(android.graphics.Color.TRANSPARENT))
        val view = ComposeView(context).apply {
            setContent(content)
        }
        setContentView(view)
    }
}


// A function that returns an android dialog based on entered content
@Composable
fun androidDialog(content: @Composable () -> Unit): AndroidComposeDialog<*>? {
    val context = LocalContext.current
    val activity = context as? AppCompatActivity
    val componentActivity = context as? ComponentActivity
    val dialog = remember(activity, content.hashCode()) {
        if (activity != null)
            AndroidComposeDialog(activity = activity, content = content)
        else if (componentActivity != null)
            AndroidComposeDialog(activity = componentActivity, content = content)
        else null
    }
    return dialog
}

现在它已经准备好了

代码语言:javascript
运行
复制
@Composable
fun ShowDialog() {
    val dialog = androidDialog {
        Box(
            modifier = Modifier
                .size(100.dp)
                .clip(RoundedCornerShape(25.dp))
                .background(Color.Blue)
        )
    }
    Box(modifier = Modifier.fillMaxSize()) {
        Button(onClick = {
            dialog?.show()
        }, modifier = Modifier.align(Alignment.Center)) {
            Text(text = "show dialog")
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68395975

复制
相关文章

相似问题

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