android - 添加到 FragmentContainerView 的视图必须与 Fragment 关联
问题描述
由于在两个片段之间的工具栏中实现后退按钮存在困难,我决定开始熟悉 Navigation Graph。但我得到一个错误
Views added to a FragmentContainerView must be associated with a Fragment. View android.widget.RelativeLayout{...} is not associated with a Fragment.
这是 .xml 文件,其中包含nav_host_fragment
:
<?xml version="1.0" encoding="utf-8"?>
<layout ...
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlack"
android:clickable="@{viewModel.isSearching ? false : true}"
android:focusable="@{viewModel.isSearching ? false : true}">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recent_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="4dp"
android:background="@color/colorBlack"
tools:itemCount="10"
tools:listitem="@layout/search_user_layout" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/search_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="4dp"
android:background="@color/colorBlack"
android:visibility="gone"
tools:itemCount="10"
tools:listitem="@layout/search_user_layout" />
<ProgressBar
android:id="@+id/loading"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="true"
android:visibility="@{viewModel.isSearching ? View.VISIBLE : View.GONE}" />
</RelativeLayout>
</fragment>
</layout>
有可能修复它吗?
解决方案
检查您的 Fragment 的onCreateView()
Layout inflate 。确保将attachToRoot设置为false
inflater.inflate(R.layout.fragment_layout, container, false)
它对我有用。
推荐阅读
- php - 如果 COUNT = 0,如何在 mySQL 中插入或更新查询
- vue.js - 未注册的动态和命名空间模块(vuex-module-decorators、vuex-class)
- php - 使用 PHP 在数组中搜索项目
- java - 为什么我不能创建本体?
- java - 403 访问被拒绝 Spring Security 每次
- mongodb - 如何在 Windows 中播种 docker 容器
- ab-initio - 我有 4 路并行 i/p 文件,6 路并行 i/p 文件。我想将数据写入 8 路多文件。你怎么能这样做?
- dolphindb - 如何修改 DolphinDB 表的列中的值
- python - 如何将一个字段的值转移到另一个具有计算和逆属性的字段(Odoo11)?
- typescript - 打字稿:将字符串数组转换为字符串文字