【问题标题】:How can I combine my BottomNavigationView with Navigation Drawer如何将我的 BottomNavigationView 与 Navigation Drawer 结合使用
【发布时间】:2020-06-22 07:08:36
【问题描述】:

因为我在我的应用程序中同时使用了 BottomNavigationView 和 NavigationDrawer 但是当我将主页按钮放入它们(BottomNavigationView和NavigationDrawer)时出现问题,导航抽屉的按钮效果很好但是当从BottomNavigationView使用主页(仪表板)时,当我点击任何卡片视图时出现问题从 BottomNavigationView 选择 Home 后的 cardView 然后应用程序崩溃,但当我从 NavigationDrawer 打开它时运行良好。

这是错误:

java.lang.IllegalArgumentException:导航目的地 com.appsnipp.admin:id/action_nav_home_to_nav_resource 对此 NavController 是未知的

应用图片

从底部导航视图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>

底部导航菜单文件

<?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);
      }

主页片段文件

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

如果需要任何其他文件代码,请告诉我。

提前致谢!

【问题讨论】:

  • 错误是什么?
  • 请发布崩溃日志。
  • @MohammadMoeinGolchin 当我在 BottomNavigationView 中按下主页后尝试使用主页时,然后当我调用任何 onClick 时,我的应用程序崩溃但在从 NavigationDrawer 按下主页后使用它完全正常。
  • @Vall0n 这是错误所在,还要检查我刚刚发布的 HomeFragment 文件。 java.lang.IllegalArgumentException:导航目的地 com.appsnipp.admin:id/action_nav_home_to_nav_resource 对此 NavController 未知
  • 当您尝试从一个片段 A 导航到片段 B 但未定义路线时,可能会发生此错误。请检查您尝试从中导航到 nav_resource 的当前片段,因为您仅在 homefragment 中定义了操作。

标签: java android xml navigation-drawer bottomnavigationview


【解决方案1】:

所以问题在于我在导航文件中创建了一个重复的片段作为nav_homenavB_home,但是在导航时我总是从nav_home 导航所以为了克服这个问题我从导航和菜单中删除了navB_home并在那里膨胀nav_home(底部导航视图) 所以现在 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>

【讨论】:

    猜你喜欢
    • 2021-12-11
    • 2019-05-20
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 2013-11-29
    相关资源
    最近更新 更多