一个FragmentContainerView中添加的视图必须与一个Fragment关联。

8
由于在两个片段之间的工具栏中实现后退按钮存在困难,我决定开始了解导航图。但是我遇到了一个错误。
“添加到FragmentContainerView的视图必须与Fragment相关联。 View android.widget.RelativeLayout{...}未与Fragment关联。”
以下是带有“nav_host_fragment”的.xml文件:
<?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>

能否修复它?


1
为什么你要在<fragment>标签内添加其他视图,而不是在外面添加? - ianhanniballake
感谢您的评论。我这样做是因为如果我去掉<fragment>,就会出现错误只允许一个具有1个视图子元素的布局元素 - MaxB
而且,如果我将<fragment>放在<RelativeLayout>中,我会收到一个错误消息:View androidx.recyclerview.widget.RecyclerView{...} is not associated with a Fragment - MaxB
我认为你需要决定哪些视图应该是你的活动的一部分(即具有NavHostFragment的那个),以及当你转到特定片段时应该膨胀哪些视图(看起来像是当前在你的<fragment>标签内的所有内容) - 这应该是一个单独的布局文件。 - ianhanniballake
相关问题:https://dev59.com/IFIG5IYBdhLWcg3w-W-R - Bill Mei
1个回答

11

检查你的Fragment的onCreateView()中的布局填充。确保将attachToRoot设置为false。

inflater.inflate(R.layout.fragment_layout, container, false)

这对我有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接