为什么底部导航片段会变成黑色并且无法在android中使用?

问题描述

enter image description here

我不知道为什么片段不起作用。我正在创建底部导航。我的代码在这里。请解决我的问题。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/draw">


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bot_nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_navigation_menu" />

这是片段部分。请解决我的问题。该应用程序无法正常运行。 这是片段部分。请解决我的问题。该应用无法正常运行。

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/bot_nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/bot_navigation"
        />



    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:fitsSystemWindows="false"
            app:popupTheme="@style/AppTheme.AppBarOverlay." />

    </com.google.android.material.appbar.AppBarLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

这是bot_navigation.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/bot_navigation"
    tools:ignore="Unusednavigation">
    <fragment
        android:id="@+id/navigation_Feed"
        android:name="com.example.reducestress.ui.Feed.Feed"
        android:label="@string/title_Feed"
        tools:layout="@layout/Feed_fragment" />

    <fragment
        android:id="@+id/navigation_diary"
        android:name="com.example.reducestress.ui.diary.diary"
        android:label="@string/title_diary"
        tools:layout="@layout/diary_fragment" />

    <fragment
        android:id="@+id/navigation_profile"
        android:name="com.example.reducestress.ui.profile.profile"
        android:label="@string/title_profile"
        tools:layout="@layout/profile_fragment" />
</navigation>

解决方法

使用新的JetPack导航,您需要将要显示的片段添加到NavGraph中。您可以从navigation目录的res目录下找到NavGraph。就您而言,您的NavGraph是bot_navigation.xml

如果您没有看到要显示的片段,则一旦您进入NavGraph,就会出现此图标

enter image description here

单击它,您将看到可用片段和活动的列表。只需单击所需的那个。

如果已经放置了片段,请确保ID与bottom_navigation_menu.xml中的ID匹配。另外,请确保在主持人活动中使用正确的Navigation和正确的NavController来设置BottomNavigationView

让我知道是否有帮助。

,

尝试将时变黑

,

您忘记了 navigation app: startDestination = "@ id / navigation_feed ,而不是Navigation_feed放了应该首先启动的片段,我认为您应该删除未使用的Navigation >

代码应类似于:

<?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/bot_navigation"
    app:startDestination="@id/navigation_feed">
...
</navigation>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...