我在主菜单类中使用BottomNavigation在主屏幕中循环浏览片段。奇怪的是我的应用程序崩溃了,因为NavController在四个片段之一中找不到目标。 MainMenuFragment与MainMenu不同(活动包含片段和NavHostFragment)。知道为什么会这样吗?
NavGraph: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中创建:
bundle.putString("UID", UID);
controller = Navigation.findNavController(this, R.id.fragment_container);
controller.navigate(R.id.mainMenuFragment, bundle);
NavGraph 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>
答案 0 :(得分: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