我如何将我的BottomNavigationView与导航抽屉结合在一起

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

当我在应用程序中同时使用BottomNavigationView和NavigationDrawer时但是当我在两个按钮(BottomNavigationView和NavigationDrawer)中都放置Home按钮时,会出现问题,导航抽屉的按钮工作正常,但是当使用BottomNavigationView中的Home(仪表板)时,当我单击任何一个卡片视图时都会出现问题从BottomNavigationView选择“主页”后,cardView会崩溃,但是当我从NavigationDrawer中打开它时,它运行良好。

这里是错误:

java.lang.IllegalArgumentException:导航目标com.appsnipp.admin:id / action_nav_home_to_nav_resource对于此NavController未知

应用程序的图片

从BottomNavigationView BottomNavigationView的主页查看

导航抽屉Navigation Drawer这是我的密码!

导航文件

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

    <fragment
        android:id="@+id/nav_home"
        android:name="com.appsnipp.admin.Navigation_Profile.ui.dashboard.DashBoardFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_dashboard" >
        <action
            android:id="@+id/action_nav_home_to_nav_account2"
            app:destination="@id/nav_account" />
        <action
            android:id="@+id/action_nav_home_to_navB_building2"
            app:destination="@id/navB_building" />
        <action
            android:id="@+id/action_nav_home_to_navB_notice3"
            app:destination="@id/navB_notice" />
        <action
            android:id="@+id/action_nav_home_to_nav_resource"
            app:destination="@id/nav_resource" />
        <action
            android:id="@+id/action_nav_home_to_nav_complain"
            app:destination="@id/nav_complain" />
        <action
            android:id="@+id/action_nav_home_to_navB_profile"
            app:destination="@id/navB_profile" />
        <action
            android:id="@+id/action_nav_home_to_nav_visitor"
            app:destination="@id/nav_visitor" />
        <action
            android:id="@+id/action_nav_home_to_nav_election"
            app:destination="@id/nav_election" />
        <action
            android:id="@+id/action_nav_home_to_nav_document"
            app:destination="@id/nav_document" />
        <action
            android:id="@+id/action_nav_home_to_nav_event"
            app:destination="@id/nav_event" />
        <action
            android:id="@+id/action_nav_home_to_nav_member"
            app:destination="@id/nav_member" />
    </fragment>
    <fragment
        android:id="@+id/navB_home"
        android:name="com.appsnipp.admin.Navigation_Profile.ui.dashboard.DashBoardFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_dashboard" />
    <fragment
        android:id="@+id/navB_building"
        android:name="com.appsnipp.admin.Navigation_Profile.ui.buildingdetails.BuildingDetailsFragment"
        android:label="@string/menu_buiding"
        tools:layout="@layout/fragment_buildingdetails" />
    <fragment
        android:id="@+id/navB_notice"
        android:name="com.appsnipp.admin.Navigation_Profile.ui.noticeboard.NoticeBoardFragment"
        android:label="@string/menu_notice"
        tools:layout="@layout/fragment_notice" />

    <fragment
        android:id="@+id/navB_profile"
        android:name="com.appsnipp.admin.Navigation_Profile.ui.profile.ProfileFragment"
        android:label="Profile"
        tools:layout="@layout/fragment_profile" />
    <fragment
        android:id="@+id/nav_account"
        android:name="com.appsnipp.admin.Navigation_Profile.ui.account.AccountFragment"
        android:label="@string/menu_account"
        tools:layout="@layout/fragment_account" />

    <fragment
        android:id="@+id/nav_member"
        android:name="com.appsnipp.admin.Navigation_Profile.ui.members.MembersFragment"
        android:label="@string/menu_member"
        tools:layout="@layout/fragment_member" />

    <fragment
        android:id="@+id/nav_election"
        android:name="com.appsnipp.admin.Navigation_Profile.ui.electionandpoll.ElectionFragment"
        android:label="@string/menu_election"
        tools:layout="@layout/fragment_election" />

    <fragment
        android:id="@+id/nav_document"
        android:name="com.appsnipp.admin.Navigation_Profile.ui.document.DocumentFragment"
        android:label="@string/menu_document"
        tools:layout="@layout/fragment_document" />

    <fragment
        android:id="@+id/nav_resource"
        android:name="com.appsnipp.admin.Navigation_Profile.ui.resource.ResourceFragment"
        android:label="@string/menu_resource"
        tools:layout="@layout/fragment_resources" />

     <fragment
        android:id="@+id/nav_visitor"
        android:name="com.appsnipp.admin.Navigation_Profile.ui.visitor.VisitorFragment"
        android:label="@string/menu_visitor"
        tools:layout="@layout/fragment_visitor" />

    <fragment
        android:id="@+id/nav_event"
        android:name="com.appsnipp.admin.Navigation_Profile.ui.event.EventFragment"
        android:label="@string/menu_event"
        tools:layout="@layout/fragment_event" />
    <fragment
        android:id="@+id/nav_complain"
        android:name="com.appsnipp.admin.Navigation_Profile.ui.complain.ComplainFragment"
        android:label="@string/menu_complain"
        tools:layout="@layout/fragment_complain" />
    </navigation>

BottomNavigationMenu文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navB_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="Home" />
    <item
        android:id="@+id/navB_building"

        android:icon="@drawable/icon_building_black"
        android:title="Building Details" />


    <item
        android:id="@+id/navB_notice"
        android:icon="@drawable/icon_notice_black"
        android:title="NoticeBoard" />

    <item
        android:id="@+id/navB_profile"
        android:icon="@drawable/ic_person_outline_black_24dp"
        android:title="Profile" />
</menu>

NavigationDrawer菜单文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
         tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/icon_home"
            android:title="@string/menu_home"
            />
        <item
            android:id="@+id/nav_account"
            android:icon="@drawable/icon_account"
            android:title="@string/menu_account" />
        <item
            android:id="@+id/nav_member"
            android:icon="@drawable/icon_member"
            android:title="@string/menu_member" />
        <item
            android:id="@+id/nav_election"
            android:icon="@drawable/icon_election"
            android:title="@string/menu_election" />
        <item
            android:id="@+id/nav_document"
            android:icon="@drawable/icon_document"
            android:title="@string/menu_document" />
        <item
            android:id="@+id/nav_resource"
            android:icon="@drawable/icon_resource"
            android:title="@string/menu_resource" />


        <item
            android:id="@+id/nav_visitor"
            android:icon="@drawable/icon_visitor"
            android:title="@string/menu_visitor" />


        <item
            android:id="@+id/nav_event"
            android:icon="@drawable/icon_event"
            android:title="@string/menu_event" />

        <item
            android:id="@+id/nav_complain"
            android:icon="@drawable/icon_complain"
            android:title="@string/menu_complain" />
    </group>
</menu>

NavigationDrawerActivity在这里完成了BottomNavigationView和NavigationDrawer的代码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_);
      ...
      DrawerLayout drawer = findViewById(R.id.drawer_layout);
         navigationView =findViewById(R.id.nav_view);
        bottomNavigationView = findViewById(R.id.nav_view_bottom);

        CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigationView.getLayoutParams();
        layoutParams.setBehavior(new BottomNavigationBehaviour());

        bottomNavigationView.setSelectedItemId(R.id.navB_home);
        navigationView=findViewById(R.id.nav_view);
        navigationView.setCheckedItem(R.id.nav_home);

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_account, R.id.nav_member,
                R.id.nav_election, R.id.nav_document, R.id.nav_resource,R.id.navB_home, 
                R.id.nav_complain,R.id.navB_notice,
                R.id.navB_building,R.id.navB_profile,
                R.id.nav_event,R.id.nav_visitor)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(bottomNavigationView,navController);
        NavigationUI.setupWithNavController(navigationView, navController);
      }

HomeFragment文件

public class DashBoardFragment extends Fragment {
  CardView c1,c2,c3,c4,c5,c6,c7,c8;
  FragmentManager manager;
  NavigationView navigationView;
  private DashBoardViewModel dashBoardViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    dashBoardViewModel =
            ViewModelProviders.of(this).get(DashBoardViewModel.class);
    View root = inflater.inflate(R.layout.fragment_dashboard, container, false);

    navigationView =root.findViewById(R.id.nav_view);
    manager=getFragmentManager();
    c1=(CardView) root.findViewById(R.id.card_account);
    c2=(CardView) root.findViewById(R.id.card_resource);
    c3=(CardView) root.findViewById(R.id.card_document);
    c4=(CardView) root.findViewById(R.id.card_member);
    c5=(CardView) root.findViewById(R.id.card_event);
    c6=(CardView) root.findViewById(R.id.card_election);
    c7=(CardView) root.findViewById(R.id.card_complain);
    c8=(CardView) root.findViewById(R.id.card_visitor);
    c1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_account2);
        }
    });
    c2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_resource);
        }
    });
    c3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_document);
        }
    });
    c4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_member);
        }
    });
    c5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_event);
        }
    });
    c6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_election);
        }
    });
    c7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_complain);
        }
    });
    c8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigate(R.id.action_nav_home_to_nav_visitor);
        }
    });
    return root;
}
}

让我知道是否需要其他文件代码。

预先感谢!

java android xml navigation-drawer bottomnavigationview
1个回答
0
投票

所以问题出在我在导航文件中以nav_home navB_home创建了一个重复片段的地方,但是在导航时我总是从nav_home导航。因此,为了解决这个问题,我从导航和菜单中删除了navB_home,在那里充气nav_home(BottomNavigationView)因此,目前NavigationView和BottomNavigationView的片段导航与nav_home相同。

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

<fragment
    android:id="@+id/nav_home" android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.dashboard.DashBoardFragment"
    android:label="@string/menu_home"
    tools:layout="@layout/fragment_dashboard" >
    <action
        android:id="@+id/action_nav_home_to_nav_account2"
        app:destination="@id/nav_account" />
    <action
        android:id="@+id/action_nav_home_to_navB_building2"
        app:destination="@id/navB_building" />
    <action
        android:id="@+id/action_nav_home_to_navB_notice3"
        app:destination="@id/navB_notice" />
    <action
        android:id="@+id/action_nav_home_to_nav_resource"
        app:destination="@id/nav_resource" />
    <action
        android:id="@+id/action_nav_home_to_nav_complain"
        app:destination="@id/nav_complain" />
    <action
        android:id="@+id/action_nav_home_to_navB_profile"
        app:destination="@id/navB_profile" />
    <action
        android:id="@+id/action_nav_home_to_nav_visitor"
        app:destination="@id/nav_visitor" />
    <action
        android:id="@+id/action_nav_home_to_nav_election"
        app:destination="@id/nav_election" />
    <action
        android:id="@+id/action_nav_home_to_nav_document"
        app:destination="@id/nav_document" />
    <action
        android:id="@+id/action_nav_home_to_nav_event"
        app:destination="@id/nav_event" />
    <action
        android:id="@+id/action_nav_home_to_nav_member"
        app:destination="@id/nav_member" />
</fragment>
<fragment
    android:id="@+id/navB_building"
   android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.buildingdetails.BuildingDetailsFragment"
    android:label="@string/menu_buiding"
    tools:layout="@layout/fragment_buildingdetails" />
<fragment
    android:id="@+id/navB_notice"
    android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.noticeboard.NoticeBoardFragment"
    android:label="@string/menu_notice"
    tools:layout="@layout/fragment_notice" />

<fragment
    android:id="@+id/navB_profile"
    android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.profile.ProfileFragment"
    android:label="Profile"
    tools:layout="@layout/fragment_profile" />
<fragment
    android:id="@+id/nav_account"
    android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.account.AccountFragment"
    android:label="@string/menu_account"
    tools:layout="@layout/fragment_account" />

<fragment
    android:id="@+id/nav_member"
    android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.members.MembersFragment"
    android:label="@string/menu_member"
    tools:layout="@layout/fragment_member" />

<fragment
    android:id="@+id/nav_election"
    android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.electionandpoll.ElectionFragment"
    android:label="@string/menu_election"
    tools:layout="@layout/fragment_election" />

<fragment
    android:id="@+id/nav_document"
    android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.document.DocumentFragment"
    android:label="@string/menu_document"
    tools:layout="@layout/fragment_document" />

<fragment
    android:id="@+id/nav_resource"
    android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.resource.ResourceFragment"
    android:label="@string/menu_resource"
    tools:layout="@layout/fragment_resources" />
<fragment
    android:id="@+id/nav_visitor"
    android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.visitor.VisitorFragment"
    android:label="@string/menu_visitor"
    tools:layout="@layout/fragment_visitor" />
<fragment
    android:id="@+id/nav_event"
    android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.event.EventFragment"
    android:label="@string/menu_event"
    tools:layout="@layout/fragment_event" />

<fragment
    android:id="@+id/nav_complain"
    android:name="com.appsnipp.loginsamples.Navigation_Profile.ui.complain.ComplainFragment"
    android:label="@string/menu_complain"
    tools:layout="@layout/fragment_complain" />
</navigation>
© www.soinside.com 2019 - 2024. All rights reserved.