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

如何解决为什么底部导航片段会变成黑色并且无法在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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?