【问题标题】:Android Navigation Graph not displaying start fragmentAndroid导航图不显示开始片段
【发布时间】:2021-07-23 03:35:56
【问题描述】:

我在调试一个应该很简单的问题时遇到了麻烦:我有一个带有一个活动的应用程序,其中包含一个导航图,它应该在启动时显示一个片段。但事实并非如此。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph"/>

</FrameLayout>

res/navigation/nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/listFragment"
    >

    <fragment
        android:id="@+id/listFragment"
        android:name="org.example.my_app.ui.ListFragment"
        android:label="fragment_list"
        tools:layout="@layout/fragment_list"
        >
        <action
            android:id="@+id/action_listFragment_to_detailFragment"
            app:destination="@id/detailFragment"
            />
    </fragment>
    <fragment
        android:id="@+id/detailFragment"
        android:name="org.example.my_app.ui.DetailFragment"
        android:label="fragment_detail"
        tools:layout="@layout/fragment_detail"
        />
</navigation>

fragment_list.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.ListFragment"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="This should be visible"
        />

</FrameLayout>

ListFragment.kt

class ListFragment : Fragment(R.layout.fragment_list)

尽管设置如此简单,但我看到了一个空白屏幕(只是一个应用栏),并且从未调用过添加到 ListFragment 类 init 的调试侦听器。为什么我的navGraph 没有初始化片段的实例?

【问题讨论】:

    标签: android android-fragments android-architecture-navigation android-navigation-graph


    【解决方案1】:

    文档:

    NavHostFragment 在您的布局中提供一个区域以进行独立导航。

    您没有通过android:name 属性在FragmentContainerView 中将片段占位符声明为NavHostFragment;因此导航不会发生。

    <androidx.fragment.app.FragmentContainerView
        ....
        android:name="androidx.navigation.fragment.NavHostFragment"
    

    【讨论】:

    • 是的,就是这样。谢谢,我盯着那段代码看了好几个小时,试图找出丢失的东西。
    猜你喜欢
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 2019-12-07
    • 1970-01-01
    相关资源
    最近更新 更多