Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android fragments 在嵌套导航图中替换FragmentContainerView上的片段时出现问题_Android Fragments_Android Navigation_Android Navigationview_Android Navigation Graph_Android Components - Fatal编程技术网

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

Android fragments 在嵌套导航图中替换FragmentContainerView上的片段时出现问题,android-fragments,android-navigation,android-navigationview,android-navigation-graph,android-components,Android Fragments,Android Navigation,Android Navigationview,Android Navigation Graph,Android Components,您好,我只有一个活动和两个导航图。第一个图包含三个片段LoginFragment、RegisterFragment和HomeFragment。在我的第二张图中有三个片段,分别是片段1、片段2和片段3。我在HomeFragment中实现了FragmentContainerView和底部导航,以便通过按底部导航图标替换容器上的第二个图形片段。但是我不知道怎么做。。我尝试了很多东西,但都没有成功,因为我不擅长编程。我把我的代码贴在下面,请检查一下。谢谢 第一个片段容器视图 <androidx.

您好,我只有一个活动和两个导航图。第一个图包含三个片段LoginFragment、RegisterFragment和HomeFragment。在我的第二张图中有三个片段,分别是片段1、片段2和片段3。我在HomeFragment中实现了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" />


<?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>

登录片段

   

     public class LoginFragment extends Fragment {
        
            private TextView sign_up_login;
            private MaterialButton btn_login;
        
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
                return inflater.inflate(R.layout.fragment_login, viewGroup, 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);
                });
            }


<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>

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, ViewGroup viewGroup, Bundle savedInstanceState) {
                return inflater.inflate(R.layout.fragment_login, viewGroup, 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**