1

我在主菜单类中使用 BottomNavigation 在主屏幕中循环浏览片段。奇怪的是我的应用程序崩溃了,因为 NavController 没有在四个片段之一中找到目的地。MainMenuFragment 与 MainMenu(包含片段和 NavHostFragment 的活动)不同。知道为什么会这样吗?

导航图: http ://prntscr.com/pcfufk

活动(MainMenu.java)画面http://prntscr.com/pcgcnv

在 MainMenu.java 中:

private BottomNavigationView.OnNavigationItemSelectedListener listener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            Bundle args = new Bundle();
            switch (menuItem.getItemId()) {
                case R.id.addorder:
                    args.putString("UID", UID);
                    controller.navigate(R.id.action_mainMenuFragment_to_addOrderFragment, args);
                    break;
                case R.id.addcust:
                    bundle.putString("UID", UID);
                    controller.navigate(R.id.action_mainMenuFragment_to_addCustomerFragment);
                    break;
                case R.id.barcodeScan:
                    controller.navigate(R.id.action_mainMenuFragment_to_addProductFragment);
                    break;
                case R.id.home:
                    bundle.putString("UID", UID);
                    if (controller.getCurrentDestination() != null && controller.getCurrentDestination().getId() == R.id.mainMenuFragment) {
                        controller.navigate(R.id.mainMenuFragment, bundle);
                    }
                    break;
            }
            return true;
        }
    };

MainMenu.java 中的 onCreate:

bundle.putString("UID", UID);
        controller = Navigation.findNavController(this, R.id.fragment_container);
        controller.navigate(R.id.mainMenuFragment, bundle);

导航图 xml

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

    <fragment
        android:id="@+id/addCustomerFragment"
        android:name="com.google.android.gms.samples.vision.barcodereader.fragments.customer.addCustomerFragment"
        android:label="addcustomerfragment"
        tools:layout="@layout/addcustomerfragment" />
    <fragment
        android:id="@+id/mainMenuFragment"
        android:name="com.google.android.gms.samples.vision.barcodereader.fragments.mainmenu.mainMenuFragment"
        android:label="mainmenufragment"
        tools:layout="@layout/mainmenufragment">
        <action
            android:id="@+id/action_mainMenuFragment_to_addProductFragment"
            app:destination="@id/addProductFragment"
            app:enterAnim="@anim/fui_slide_in_right"
            app:exitAnim="@anim/fui_slide_out_left"
            app:popExitAnim="@anim/fui_slide_out_left"
            app:popUpTo="@+id/mainMenuFragment" />
        <action
            android:id="@+id/action_mainMenuFragment_self"
            app:destination="@id/mainMenuFragment" />

        <action
            android:id="@+id/action_mainMenuFragment_to_addOrderFragment"
            app:destination="@id/addOrderFragment"
            app:enterAnim="@anim/fui_slide_in_right"
            app:exitAnim="@anim/fui_slide_out_left"
            app:popUpTo="@+id/mainMenuFragment" />
        <argument
            android:name="UID"
            app:argType="string"
            app:nullable="false" />
        <action
            android:id="@+id/action_mainMenuFragment_to_addCustomerFragment"
            app:destination="@id/addCustomerFragment"
            app:enterAnim="@anim/fui_slide_in_right"
            app:exitAnim="@anim/fui_slide_out_left"
            app:popUpTo="@+id/navigation_mainmenu" />
    </fragment>
    <fragment
        android:id="@+id/addProductFragment"
        android:name="com.google.android.gms.samples.vision.barcodereader.fragments.products.addProductFragment"
        android:label="addproductfragment"
        tools:layout="@layout/addproductfragment" />
    <fragment
        android:id="@+id/additionalInfoFragment"
        android:name="com.google.android.gms.samples.vision.barcodereader.fragments.orders.additionalInfoFragment"
        android:label="addinfofragment"
        tools:layout="@layout/addinfofragment">
        <argument
            android:name="UID"
            app:argType="string" />
    </fragment>
    <fragment
        android:id="@+id/addOrderFragment"
        android:name="com.google.android.gms.samples.vision.barcodereader.fragments.orders.addOrderFragment"
        android:label="choosecustomerfragment"
        tools:layout="@layout/choosecustomerfragment">
        <action
            android:id="@+id/action_addOrderFragment_to_additionalInfoFragment"
            app:destination="@id/additionalInfoFragment"
            app:enterAnim="@anim/fui_slide_in_right"
            app:exitAnim="@anim/fui_slide_out_left"
            app:popUpTo="@+id/mainMenuFragment" />
        <argument
            android:name="UID"
            app:argType="string" />
    </fragment>
4

1 回答 1

0

使用带有 NavigationUI 的 BottomNavigationView 和 NavigationUI.setupWithNavigationController(controller) 应该可以解决问题。

来源链接:
https ://medium.com/@vepetruskova/the-new-android-in-app-navigation-f7bfbe925b9 https://developer.android.com/guide/navigation/navigation-ui

于 2019-09-29T14:52:24.153 回答