在嵌套导航图中替换FragmentContainerView上的片段时出现问题

问题描述

您好,我只有一个活动和两个导航图。第一个图包含三个片段LoginFragment,RegisterFragment和HomeFragment。在我的第二张图中,有3个三个片段,分别是Fragment1,Fragment2和Fragment3。我在HomeFragment中实现了FragmentContainerView和底部导航,以便通过按底部导航图标替换该容器上的Second Graph Fragments。但是我不知道该怎么做..我尝试了很多事情,但是没有成功,因为我编程不好。我在下面粘贴了我的代码,请检查一下。谢谢

一个FragmentContainerView


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.StartActivity">

<androidx.fragment.app.FragmentContainerView
    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:navGraph="@navigation/nav_graph" />

第二个FragmentContainerView


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Fragments.HomeFragment">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/second_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottomnavigation"
        app:defaultNavHost="true"
        app:navGraph="@navigation/second_nav_graph" />

    <com.etebarian.meowbottomnavigation.MeowBottomNavigation
        android:id="@+id/bottomnavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:mbn_backgroundBottomColor="@color/purple_500"
        app:mbn_circleColor="@color/purple_700"
        app:mbn_countBackgroundColor="#ff6f00"
        app:mbn_countTextColor="@color/purple_700"
        app:mbn_defaultIconColor="@android:color/white"
        app:mbn_rippleColor="@color/lime_A700"
        app:mbn_selectedIconColor="@color/lime_A700"
        app:mbn_shadowColor="#1f212121" />

</RelativeLayout>

一个导航图

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

    <include app:graph="@navigation/second_nav_graph" />
    <fragment
        android:id="@+id/loginFragment"
        android:name="com.eclairios.socialconnectivity.Fragments.LoginFragment"
        android:label="fragment_login"
        tools:layout="@layout/fragment_login" >
        <action
            android:id="@+id/action_loginFragment_to_registerFragment"
            app:destination="@id/registerFragment"
            app:enteranim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnteranim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right" />
        <action
            android:id="@+id/action_loginFragment_to_homeFragment"
            app:destination="@id/homeFragment" />
    </fragment>
    <fragment
        android:id="@+id/registerFragment"
        android:name="com.eclairios.socialconnectivity.Fragments.RegisterFragment"
        android:label="fragment_register"
        tools:layout="@layout/fragment_register" >
        <action
            android:id="@+id/action_registerFragment_to_homeFragment"
            app:destination="@id/homeFragment" />
    </fragment>

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.eclairios.socialconnectivity.Fragments.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" >

        <action
            android:id="@+id/action_homeFragment_to_second_nav_graph2"
            app:destination="@id/second_nav_graph" />
        <action
            android:id="@+id/action_homeFragment_to_second_nav_graph3"
            app:destination="@id/second_nav_graph" />
    </fragment>

</navigation>

第二个导航图


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

   <fragment
       android:id="@+id/firstFragment"
       android:name="com.eclairios.socialconnectivity.Fragments.FirstFragment"
       android:label="fragment_first"
       tools:layout="@layout/fragment_first" >
       <action
           android:id="@+id/action_firstFragment_to_secondFragment"
           app:destination="@id/secondFragment" />
       <action
           android:id="@+id/action_firstFragment_to_thirdFragment"
           app:destination="@id/thirdFragment" />
   </fragment>
   <fragment
       android:id="@+id/secondFragment"
       android:name="com.eclairios.socialconnectivity.Fragments.SecondFragment"
       android:label="fragment_second"
       tools:layout="@layout/fragment_second" >
       <action
           android:id="@+id/action_secondFragment_to_thirdFragment"
           app:destination="@id/thirdFragment" />
       <action
           android:id="@+id/action_secondFragment_to_firstFragment"
           app:destination="@id/firstFragment" />
       <action
           android:id="@+id/action_secondFragment_to_thirdFragment2"
           app:destination="@id/thirdFragment" />
   </fragment>
   <fragment
       android:id="@+id/thirdFragment"
       android:name="com.eclairios.socialconnectivity.Fragments.ThirdFragment"
       android:label="fragment_third"
       tools:layout="@layout/fragment_third" >
       <action
           android:id="@+id/action_thirdFragment_to_firstFragment"
           app:destination="@id/firstFragment" />
       <action
           android:id="@+id/action_thirdFragment_to_secondFragment"
           app:destination="@id/secondFragment" />
   </fragment>
</navigation>

这是包含第二个FragmentContainerView和底部导航的片段

public class HomeFragment extends Fragment {

    private static final int TAB_ID = 1;
    private MeowBottomNavigation bottomNavigation;
    Fragment nestednavHostFragment;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup viewGroup,Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home,viewGroup,false);
    }

    @Override
    public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);

        init(view);


        listner_SignUp(view);
    }

    private void init(View view) {
        bottomNavigation = view.findViewById(R.id.bottomnavigation);
        bottomNavigation.show(TAB_ID,false);
        bottomNavigation.add(new MeowBottomNavigation.Model(1,R.drawable.ic_invisible));
        bottomNavigation.add(new MeowBottomNavigation.Model(2,R.drawable.ic_visibility));
        bottomNavigation.add(new MeowBottomNavigation.Model(3,R.drawable.email));

    }

    private void listner_SignUp(View view){
        bottomNavigation.setonClickMenuListener(new MeowBottomNavigation.ClickListener() {
            @Override
            public void onClickItem(MeowBottomNavigation.Model item) {
                // your codes
                if (item.getId()==1) {
                    
                    Navigation.findNavController(view).navigate(R.id.firstFragment);

                    
                }else if (item.getId()==2){
                    
                    Navigation.findNavController(view).navigate(R.id.secondFragment);

                    
                }else if (item.getId()==3){
                    
                    Navigation.findNavController(view).navigate(R.id.thirdFragment);
                    
                }
            }
        });

        bottomNavigation.setonShowListener(new MeowBottomNavigation.ShowListener() {
            @Override
            public void onShowItem(MeowBottomNavigation.Model item) {
                // your codes
                if (item.getId()==1) {
                    
                    Navigation.findNavController(view).navigate(R.id.firstFragment);
                    
                }else if (item.getId()==2){

                    Navigation.findNavController(view).navigate(R.id.secondFragment);
                    
                }else if (item.getId()==3){
                    
                    Navigation.findNavController(view).navigate(R.id.thirdFragment);
                    
                }
                Log.e("djfndjkfnv","onClickItem: showlistner "+item.getCount()+"==========="+item.getId() );
            }
        });

        bottomNavigation.setonReselectListener(new MeowBottomNavigation.ReselectListener() {
            @Override
            public void onReselectItem(MeowBottomNavigation.Model item) {
                // your codes

                Log.e("djfndjkfnv","onClickItem: reselected "+item.getCount()+"==========="+item.getId() );
            }
        });
    }
}



    

登录片段

   

     public class LoginFragment extends Fragment {
        
            private TextView sign_up_login;
            private MaterialButton btn_login;
        
            @Override
            public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
                return inflater.inflate(R.layout.fragment_login,false);
            }
        
            @Override
            public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState) {
                super.onViewCreated(view,savedInstanceState);
        
                init(view);
                listner_SignUp();
        
            }
        
            private void init(View view) {
                sign_up_login = view.findViewById(R.id.sign_up_login);
                btn_login = view.findViewById(R.id.btn_login);
            }
        
            private void listner_SignUp(){
                sign_up_login.setonClickListener(v -> {
        
                    findNavController(LoginFragment.this).navigate(R.id.registerFragment);
                });
        
                btn_login.setonClickListener(v -> {
                    findNavController(LoginFragment.this).navigate(R.id.homeFragment);
                });
            }

I got that error
**Navigation action/destination com.eclairios.socialconnectivity:id/firstFragment cannot be found from the current destination Destination(com.eclairios.socialconnectivity:id/homeFragment) label=fragment_home class=com.eclairios.socialconnectivity.Fragments.HomeFragment**

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)