底部导航未在android jet pack导航图中显示片段

时间:2020-01-08 11:38:39

标签: android android-fragments navigation bottomnavigationview

我有一个包含两个活动的应用程序,一个是登录,注册和...片段的主机,如果用户注册,它将转到另一个活动,该活动包含底部导航和5个片段进行切换。问题是第一个活动工作正常,但是当我在第二个活动中吃午饭时,底部导航显示并正常工作,但片段内容未显示! 这是我的导航图:

<?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/main_nav_graph"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.prototype.nyx.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_homeFragment_to_walletFragment"
            app:destination="@id/walletFragment" />
    </fragment>
    <fragment
        android:id="@+id/walletFragment"
        android:name="com.prototype.nyx.WalletFragment"
        android:label="fragment_wallet"
        tools:layout="@layout/fragment_wallet" >
        <action
            android:id="@+id/action_walletFragment_to_addProductFragment"
            app:destination="@id/addProductFragment" />
    </fragment>
    <fragment
        android:id="@+id/addProductFragment"
        android:name="com.prototype.nyx.AddProductFragment"
        android:label="fragment_add_product"
        tools:layout="@layout/fragment_add_product" >
        <action
            android:id="@+id/action_addProductFragment_to_settingsFragment"
            app:destination="@id/settingsFragment" />
    </fragment>
    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.prototype.nyx.SettingsFragment"
        android:label="fragment_settings"
        tools:layout="@layout/fragment_settings" >
        <action
            android:id="@+id/action_settingsFragment_to_profileFragment"
            app:destination="@id/profileFragment" />
    </fragment>
    <fragment
        android:id="@+id/profileFragment"
        android:name="com.prototype.nyx.ProfileFragment"
        android:label="fragment_profile"
        tools:layout="@layout/fragment_profile" />
</navigation>

这是我的主要活动及其布局:

package com.prototype.nyx;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpNavigation();
    }

    public void setUpNavigation() {
        bottomNavigationView = findViewById(R.id.bottom_nav);
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
                .findFragmentById(R.id.main_nav_host_fragment);
        System.out.println("number of fragments is:"+navHostFragment.getChildFragmentManager()
                .getFragments().size());
        NavigationUI.setupWithNavController(bottomNavigationView,
                navHostFragment.getNavController());
    }
}

<?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"
    android:background="@color/white"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/main_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:navGraph="@navigation/main_nav_graph" />



    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

和菜单:

    <?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/homeFragment"
        android:icon="@drawable/ic_home"
        app:showAsAction="always"
        android:title="">
    </item>


    <item
        android:id="@id/walletFragment"
        android:icon="@drawable/ic_wallet"
        app:showAsAction="always"
        android:title="">
    </item>

    <item
        android:id="@id/addProductFragment"
        android:icon="@drawable/ic_add"
        app:showAsAction="always"
        android:title="">
    </item>

    <item
        android:id="@+id/settingsFragment"
        android:icon="@drawable/ic_settings"
        app:showAsAction="always"
        android:title="">
    </item>

    <item
        android:id="@id/profileFragment"
        android:icon="@drawable/ic_person_black_24dp"
        app:showAsAction="always"
        android:title="">
    </item>
</menu>

起初我以为这是主要的活动布局,所以我也尝试了框架布局和协调器布局。 但没有任何效果。 我的片段之一:

<?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=".HomeFragment">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="256dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.063"
        tools:src="@tools:sample/avatars" />

</androidx.constraintlayout.widget.ConstraintLayout>

出什么问题了?

1 个答案:

答案 0 :(得分:2)

尝试检查片段的软件包名称和navgraph中的片段名称attr