单击BottomTabBar导航上的第一个项目在Android上不起作用

问题描述 投票:0回答:1

我正在将Android Jetpack与Kotlin一起使用NavigationHostFragment,Navgraph和NavController创建此BottomBarNavigation UI。

我有3个标签,并且除我尝试单击第一个标签外,其他所有操作均按预期进行,它没有任何作用。

卡住了几天。找不到问题。有什么想法吗?

bottom_tabbar_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"
    android:id="@+id/bottom_tabbar_nav_graph"
    app:startDestination="@id/transactionFragment">

    <fragment
        android:id="@+id/transactionFragment"
        android:name="com.example.project_budget_planner.main.transaction.TransactionFragment"
        android:label="fragment_transaction" />

    <fragment
        android:id="@+id/statisticsFragment"
        android:name="com.example.project_budget_planner.main.statistics.StatisticsFragment"
        android:label="fragment_statistics" />

    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.example.project_budget_planner.main.settings.SettingsFragment"
        android:label="fragment_settings" />
</navigation>

bottom_tabbar_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/trasactionFragment"
        android:icon="@drawable/common_google_signin_btn_icon_dark"
        android:orderInCategory="1"
        app:showAsAction="always"
        android:title="Transaction" />

    <item
        android:id="@+id/statisticsFragment"
        android:icon="@drawable/common_google_signin_btn_icon_dark"
        android:orderInCategory="2"
        app:showAsAction="always"
        android:title="Statistics" />

    <item
        android:id="@+id/settingsFragment"
        android:icon="@drawable/common_google_signin_btn_icon_dark"
        android:orderInCategory="3"
        app:showAsAction="always"
        android:title="Settings" />
</menu>

activity_main.xml

<?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=".main.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_toTopOf="@id/bottom_navbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/bottom_tabbar_nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ccc"
        app:itemIconTint="#472B78"
        app:itemTextColor="#472C67"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_tabbar_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

private fun setupNavigation() {
        val bottomNavigationView: BottomNavigationView = findViewById(R.id.bottom_navbar)
        val navHostFragment: NavHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        NavigationUI.setupWithNavController(bottomNavigationView, navHostFragment.navController)
    }
android xml android-fragments kotlin android-navigation-graph
1个回答
0
投票

您在菜单项android:id="@+id/trasactionFragment"上拼写了错误的ID。它应该是android:id="@+id/transactionFragment"(注意n)。

© www.soinside.com 2019 - 2024. All rights reserved.