java.lang.IllegalStateException:未从 androidx.constraintlayout.widget.ConstraintLayout 找到 ViewTreeLifecycleOwner

问题描述

当我尝试使用 XML 在叠加层中插入 Compose(绘制其他应用程序)时,出现此异常:

java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.constraintlayout.widget.ConstraintLayout{d596746 V.E...... ......ID 0,0-0,0}

但是没有覆盖(在活动中)它可以正常工作。有谁知道如何解决?我已经将 AppCompat 库更新到 1.3.0

我的 XML 代码

<?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>

我的叠加代码

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)

解决方法

  1. 确保您的约束布局已更新到应用级别 build.gradle 文件中的最新版本。

dependencies { ...    
 implementation 'androidx.constraintlayout:constraintlayout:1.3.x'
  1. 可以肯定的是,搜索并替换所有您的 xml 标记名称

<androidx.constraintlayout.ConstraintLayout>

 //with ->

<androidx.constraintlayout.widget.ConstraintLayout>

 

在每个地方 CTRL + SHIFT + R _

  1. gradle.properties添加这些:

 android.enableJetifier=true
 android.useAndroidX=true
  1. 清除/使缓存无效并重新启动 Android Studio。

文件无效缓存/重启无效并重启