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

ffscu2ro  于 12个月前  发布在  Android
关注(0)|答案(5)|浏览(190)

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

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)

xmjla07d

xmjla07d1#

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

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

字符串
AppCompatActivity 继承:

class MyActivity: AppCompatActivity() {
  ...
}

epfja78i

epfja78i2#

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

class Xxx : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    val view = inflater.inflate(R.layout.fragment_xxx, container, false)

    view.findViewById<ComposeView>(R.id.compose_view).apply {
        setViewCompositionStrategy(
            ViewCompositionStrategy.DisposeOnLifecycleDestroyed(viewLifecycleOwner)
        )
        setContent {
            // compose
        }
    }
    return view
}

字符串
XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <androidx.compose.ui.platform.ComposeView
        android:id="@+id/compose_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

xnifntxz

xnifntxz3#

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

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

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

<androidx.constraintlayout.ConstraintLayout>

 //with ->

<androidx.constraintlayout.widget.ConstraintLayout>


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

android.enableJetifier=true
 android.useAndroidX=true


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

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

laximzn5

laximzn54#

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

class AndroidComposeDialog<T>(
    private val activity: T,
    private val content: @Composable () -> Unit
) : Dialog(activity) where T : Context, T : androidx.lifecycle.LifecycleOwner, T : ViewModelStoreOwner, T : SavedStateRegistryOwner, T : OnBackPressedDispatcherOwner {

    private fun initViewTreeOwners() {
        val window = window ?: return
        ViewTreeLifecycleOwner.set(window.decorView, activity)
        ViewTreeViewModelStoreOwner.set(window.decorView, activity)
        window.decorView.setViewTreeSavedStateRegistryOwner(activity)
        window.decorView.setViewTreeOnBackPressedDispatcherOwner(activity)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //here i init view tree owner that will be used by compose view
        initViewTreeOwners()
        //here i hide background of dialog
        window?.setBackgroundDrawable(ColorDrawable(android.graphics.Color.TRANSPARENT))
        val view = ComposeView(context).apply {
            setContent(content)
        }
        setContentView(view)
    }
}

// A function that returns an android dialog based on entered content
@Composable
fun androidDialog(content: @Composable () -> Unit): AndroidComposeDialog<*>? {
    val context = LocalContext.current
    val activity = context as? AppCompatActivity
    val componentActivity = context as? ComponentActivity
    val dialog = remember(activity, content.hashCode()) {
        if (activity != null)
            AndroidComposeDialog(activity = activity, content = content)
        else if (componentActivity != null)
            AndroidComposeDialog(activity = componentActivity, content = content)
        else null
    }
    return dialog
}

字符串
现在可以使用了

@Composable
fun ShowDialog() {
    val dialog = androidDialog {
        Box(
            modifier = Modifier
                .size(100.dp)
                .clip(RoundedCornerShape(25.dp))
                .background(Color.Blue)
        )
    }
    Box(modifier = Modifier.fillMaxSize()) {
        Button(onClick = {
            dialog?.show()
        }, modifier = Modifier.align(Alignment.Center)) {
            Text(text = "show dialog")
        }
    }
}

zc0qhyus

zc0qhyus5#

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

contentView = ComposeView(this).apply {
    setViewTreeSavedStateRegistryOwner(this@ForegroundService)
    setViewTreeLifecycleOwner(this@ForegroundService) // <-- missing line.
    setContent {
        OverlayComposeUi()
    }
}

字符串

相关问题