【发布时间】: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/115145 和 stackoverflow.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