java.lang.IllegalStateException:在androidx.constraintlayout.widget.约束布局中找不到ViewTreeLifecleOwner

ffscu2ro  于 2024-01-04  发布在  Android
关注(0)|答案(5)|浏览(238)

当我尝试使用XML将Compose插入到overlay(绘制在其他应用程序上)时,我会遇到以下异常:

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

字符串
但是没有覆盖(在活动中)它正常工作。有人知道如何解决吗?我已经更新AppCompat库到1.3.0
我的XML代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/black">
  6. <androidx.compose.ui.platform.ComposeView
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:id="@+id/compose_view"/>
  10. </androidx.constraintlayout.widget.ConstraintLayout>


我的叠加代码:

  1. mParams = WindowManager.LayoutParams(
  2. WindowManager.LayoutParams.WRAP_CONTENT,
  3. WindowManager.LayoutParams.WRAP_CONTENT,
  4. WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
  5. WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
  6. PixelFormat.TRANSLUCENT
  7. )
  8. layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
  9. mView = layoutInflater.inflate(R.layout.power_overlay, null)
  10. mParams!!.gravity = Gravity.CENTER
  11. mWindowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
  12. mWindowManager.addView(mView, mParams)

xmjla07d

xmjla07d1#

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

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

字符串
AppCompatActivity 继承:

  1. class MyActivity: AppCompatActivity() {
  2. ...
  3. }

epfja78i

epfja78i2#

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

  1. class Xxx : Fragment() {
  2. override fun onCreateView(
  3. inflater: LayoutInflater,
  4. container: ViewGroup?,
  5. savedInstanceState: Bundle?
  6. ): View? {
  7. val view = inflater.inflate(R.layout.fragment_xxx, container, false)
  8. view.findViewById<ComposeView>(R.id.compose_view).apply {
  9. setViewCompositionStrategy(
  10. ViewCompositionStrategy.DisposeOnLifecycleDestroyed(viewLifecycleOwner)
  11. )
  12. setContent {
  13. // compose
  14. }
  15. }
  16. return view
  17. }

字符串
XML:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/white"
  6. android:orientation="vertical">
  7. <androidx.compose.ui.platform.ComposeView
  8. android:id="@+id/compose_view"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"/>
  11. </LinearLayout>

展开查看全部
xnifntxz

xnifntxz3#

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

  1. dependencies { ...
  2. implementation 'androidx.constraintlayout:constraintlayout:1.3.x'

字符串
1.为了确保这一点,请搜索并替换ALL您的xml标记名称

  1. <androidx.constraintlayout.ConstraintLayout>
  2. //with ->
  3. <androidx.constraintlayout.widget.ConstraintLayout>


在每一个地方CTRL+R_
1.在gradle.properties中添加这些:

  1. android.enableJetifier=true
  2. android.useAndroidX=true


1.清除/无效缓存并重新启动Android Studio。

文件失效缓存/重启失效重启

展开查看全部
laximzn5

laximzn54#

我写了这段代码,它工作了。

  1. class AndroidComposeDialog<T>(
  2. private val activity: T,
  3. private val content: @Composable () -> Unit
  4. ) : Dialog(activity) where T : Context, T : androidx.lifecycle.LifecycleOwner, T : ViewModelStoreOwner, T : SavedStateRegistryOwner, T : OnBackPressedDispatcherOwner {
  5. private fun initViewTreeOwners() {
  6. val window = window ?: return
  7. ViewTreeLifecycleOwner.set(window.decorView, activity)
  8. ViewTreeViewModelStoreOwner.set(window.decorView, activity)
  9. window.decorView.setViewTreeSavedStateRegistryOwner(activity)
  10. window.decorView.setViewTreeOnBackPressedDispatcherOwner(activity)
  11. }
  12. override fun onCreate(savedInstanceState: Bundle?) {
  13. super.onCreate(savedInstanceState)
  14. //here i init view tree owner that will be used by compose view
  15. initViewTreeOwners()
  16. //here i hide background of dialog
  17. window?.setBackgroundDrawable(ColorDrawable(android.graphics.Color.TRANSPARENT))
  18. val view = ComposeView(context).apply {
  19. setContent(content)
  20. }
  21. setContentView(view)
  22. }
  23. }
  24. // A function that returns an android dialog based on entered content
  25. @Composable
  26. fun androidDialog(content: @Composable () -> Unit): AndroidComposeDialog<*>? {
  27. val context = LocalContext.current
  28. val activity = context as? AppCompatActivity
  29. val componentActivity = context as? ComponentActivity
  30. val dialog = remember(activity, content.hashCode()) {
  31. if (activity != null)
  32. AndroidComposeDialog(activity = activity, content = content)
  33. else if (componentActivity != null)
  34. AndroidComposeDialog(activity = componentActivity, content = content)
  35. else null
  36. }
  37. return dialog
  38. }

字符串
现在可以使用了

  1. @Composable
  2. fun ShowDialog() {
  3. val dialog = androidDialog {
  4. Box(
  5. modifier = Modifier
  6. .size(100.dp)
  7. .clip(RoundedCornerShape(25.dp))
  8. .background(Color.Blue)
  9. )
  10. }
  11. Box(modifier = Modifier.fillMaxSize()) {
  12. Button(onClick = {
  13. dialog?.show()
  14. }, modifier = Modifier.align(Alignment.Center)) {
  15. Text(text = "show dialog")
  16. }
  17. }
  18. }

展开查看全部
zc0qhyus

zc0qhyus5#

我正在与绘制覆盖从foregound服务和我的问题是在这里:

  1. contentView = ComposeView(this).apply {
  2. setViewTreeSavedStateRegistryOwner(this@ForegroundService)
  3. setViewTreeLifecycleOwner(this@ForegroundService) // <-- missing line.
  4. setContent {
  5. OverlayComposeUi()
  6. }
  7. }

字符串

相关问题