Jetpack Compose:未找到 ViewTreeLifecycleOwner [英] Jetpack Compose : ViewTreeLifecycleOwner not found

查看:129
本文介绍了Jetpack Compose:未找到 ViewTreeLifecycleOwner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的片段中使用 Compose 时出现此错误,在 XML 的情况下工作正常

I'm getting this error while using Compose in my fragment which works fine in case of XML

未从 androidx.fragment.app.FragmentContainerView 中找到 ViewTreeLifecycleOwner

我使用的是单一活动方法没有使用 Jetpack Navigation 组件

I'm using a single activity approach without using Jetpack Navigation component

活动:

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_nav)
  supportFragmentManager.commit {
    setReorderingAllowed(true)
    add<InboxFragment>(R.id.nav_fragmentContainerView_appNav)
  }
}

<androidx.fragment.app.FragmentContainerView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/nav_fragmentContainerView_appNav"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

片段:

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
): View {
  return ComposeView(requireContext()).apply {
    setContent {
      Text(text = "HELLO FRIEND!")
    }
  }
}

依赖:

def fragment_version = "1.3.3"
implementation("androidx.fragment:fragment-ktx:$fragment_version")

def compose_version = "1.0.0-beta06"
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.activity:activity-compose:1.3.0-alpha07"

classpath "com.android.tools.build:gradle:7.0.0-alpha15"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"

推荐答案

由于您使用的是 AppCompatActivity,因此只有 appcompat 1.3 版本会填充 ViewTreeLifecycleOwner.

Since you are using an AppCompatActivity, only the appcompat 1.3 versions populate the ViewTreeLifecycleOwner.

添加:

implementation 'androidx.appcompat:appcompat:1.3.0'

这篇关于Jetpack Compose:未找到 ViewTreeLifecycleOwner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆