【问题标题】:java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.constraintlayout.widget.ConstraintLayoutjava.lang.IllegalStateException:从 androidx.constraintlayout.widget.ConstraintLayout 找不到 ViewTreeLifecycleOwner
【发布时间】:2021-09-24 11:44:40
【问题描述】:

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

【问题讨论】:

  • 请参阅 stackoverflow.com/q/65755763/115145stackoverflow.com/a/66447195/115145,了解在活动或片段(在这种情况下为 IME)之外使用 Compose 的示例。
  • 我认为您的意思是第一行中的 ConstraintLayout,而不是 Compose;请考虑删除 Android-jetpack-compose 标签
  • 更新 androidx.appcompat:appcompat 到最新版本 1.3.0
  • @MuthuramanSundararaj 已经这样做了。
  • java.lang.IllegalStateException: 表示您在未准备好时尝试做某事,这就是为什么当您删除它时它会起作用,您需要找出合适的时机来执行此操作。你可以试试 onResume 吗?

标签: android android-jetpack-compose android-overlay


【解决方案1】:
  1. 确保您的约束布局已更新到应用级别 build.gradle 文件中的最新版本。

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

<androidx.constraintlayout.ConstraintLayout>

 //with ->

<androidx.constraintlayout.widget.ConstraintLayout>

 

在每个地方 CTRL + SHIFT + R _

  1. gradle.properties 添加这些:

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

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

【讨论】:

    【解决方案2】:

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

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

    并从 AppCompatActivity 继承:

    class MyActivity: AppCompatActivity() {
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-28
      • 2021-10-18
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多