Android 如何通过单击工具栏图标导航到片段

Android 如何通过单击工具栏图标导航到片段,android,android-fragments,android-architecture-navigation,Android,Android Fragments,Android Architecture Navigation,我的应用程序栏中有一个ImageButton,现在我想在单击时导航到设置-片段 我已经为我的设置片段获得了一个导航图XML规范,如下所示: <fragment android:id="@+id/settings" android:name="try.settingsFragment" android:label="@string/settings_title" tools:layout="@layout/fragmen

我的
应用程序栏中有一个ImageButton
,现在我想在单击时导航到
设置
-片段

我已经为我的
设置
片段获得了一个
导航图XML规范
,如下所示:

    <fragment
        android:id="@+id/settings"
        android:name="try.settingsFragment"
        android:label="@string/settings_title"
        tools:layout="@layout/fragment_settings">
    </fragment>
导致:

    app:id/toolbar_settings_button} does not have a NavController set
那么,如何在我的
navigation.xml
中注册任何不是片段的内容呢?我从来没有看到任何其他指定的地方,但应用程序栏不是一个碎片

编辑

问题似乎与我的工具栏规范有关-它不在
NavHostFragment
范围内

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/appbar_include"
        app:layout_constraintVertical_bias="0.0"
        app:navGraph="@navigation/mobile_navigation">
    </fragment>

    <include
        android:id="@+id/appbar_include"
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

app:id/toolbar\u settings\u按钮
未设置导航控制器,但您的
nav\u主机\u片段
已设置

private void navigate() {
    findNavController(R.id.nav_host_fragment).navigate(R.id.action_fragmentA_to_fragmentB);
}
确保正确构建了导航图。 在navigation graph.xml中,可能有如下内容:

<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/my_test_graph"
    app:startDestination="@id/fragmentA">

    <fragment
        android:id="@+id/fragmentA"
        android:name="com.example.ui.fragmentA"
        android:label="fragment_a"
        tools:layout="@layout/fragment_a" >
        <action
            android:id="@+id/action_fragmentA_to_fragmentB"
            app:destination="@id/fragmentB" />
    </fragment>

    <fragment
        android:id="@+id/fragmentB"
        android:name="com.example.ui.fragmentB"
        android:label="fragment_b"
        tools:layout="@layout/fragment_b" >
    </fragment>
</navigation>

碎片B是您的设置碎片


操作id用于从当前片段导航到下一个片段

您是否尝试过
findNavController()。导航(navAction)
在您的click listener中?@Guillermelimapereira I稍微更新了代码-遗憾的是,结果是相同的错误。这可能是因为我忘记了生命周期的一些事情吗?因为这是第一个被初始化的活动。@Rüdiger我想知道我的答案是否适合你
private void navigate() {
    findNavController(R.id.nav_host_fragment).navigate(R.id.action_fragmentA_to_fragmentB);
}
<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/my_test_graph"
    app:startDestination="@id/fragmentA">

    <fragment
        android:id="@+id/fragmentA"
        android:name="com.example.ui.fragmentA"
        android:label="fragment_a"
        tools:layout="@layout/fragment_a" >
        <action
            android:id="@+id/action_fragmentA_to_fragmentB"
            app:destination="@id/fragmentB" />
    </fragment>

    <fragment
        android:id="@+id/fragmentB"
        android:name="com.example.ui.fragmentB"
        android:label="fragment_b"
        tools:layout="@layout/fragment_b" >
    </fragment>
</navigation>