6

当我尝试使用 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)
4

2 回答 2

0

For me it was because I had not included the appcompat library and my activity inherited from Activity instead of AppCompatActivity. The problem resolved by adding the library:

implementation("androidx.appcompat:appcompat:1.3.1")

and inheriting from AppCompatActivity:

class MyActivity: AppCompatActivity() {
  ...
}
于 2021-09-17T09:11:39.007 回答
0
  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。

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

于 2021-07-29T21:40:12.750 回答