你好,我在执行该程序时遇到很多问题。我在主要活动中有以下xml
content_main ...
<LinearLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
android:orientation="vertical"
tools:showIn="@layout/app_bar_main">
<fragment
android:id="@+id/nav_host"
android:layout_width="match_parent"
android:layout_height="0dp"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/bottom_nav_usuario_auto"
app:defaultNavHost="true"
android:layout_weight="6">
</fragment>
<FrameLayout
android:id="@+id/bottom_nav_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
/>
</LinearLayout>
如您所见,我正在根据我应用程序中的某些条件使用框架布局更改底部导航视图,我必须像4个BNViews一样使用。 因此,然后在框架布局中放置一个底部的Nav视图以开始练习。
BottomNavUsuarioAuto bottomNavUsuario=new BottomNavUsuarioAuto();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.bottom_nav_container, bottomNavUsuario);
fragmentTransaction.commit();
这是底部配置和fragment_bottom_nav_usuario_auto
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".BottomNavUsuarioAuto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/nav_usuario_auto"
android:layout_gravity="start"
android:text="@string/hello_blank_fragment" />
</LinearLayout>
</FrameLayout>
然后选择菜单...
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Inicio"
android:id="@+id/fragmento_a_usuario_auto"
android:icon="@drawable/ic_menu_camera"
android:iconTint="#FF9800"
/>
<item android:title="Mi carro"
android:icon="@drawable/ic_menu_manage"
android:id="@+id/fragmento_b_usuario_auto"
android:iconTint="#FF9800"
/>
<item android:title="Comercial"
android:id="@+id/fragmento_c_usuario_auto"
android:icon="@drawable/ic_menu_send"
android:iconTint="#FF9800"
/>
<item
android:title="Cita taller"
android:id="@+id/fragmento_d_usuario_auto"
android:icon="@drawable/ic_menu_share"
android:iconTint="#FF9800"
/>
</menu>
然后我确定导航图中的ID与菜单中的ID相匹配。
然后我确保我在MainActivity中打电话
NavController navController= Navigation.findNavController(this,R.id.nav_host);
BottomNavigationView bottomNavigationView=findViewById(R.id.bottom_nav_switcher);
NavigationUI.setupWithNavController(bottomNavigationView,navController);
我在此过程中看到两种类型的错误...
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.franco.mac.jetpacknavigation2/com.franco.mac.jetpacknavigation2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.bottomnavigation.BottomNavigationView.setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference
和:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.franco.mac.jetpacknavigationexample/com.franco.mac.jetpacknavigationexample.MainActivity}: android.view.InflateException: `Binary XML file line #11: Binary XML file line #12: Error inflating class fragment`
如果您现在真的不回答,请不要回答。我想知道完成此练习的正确方法。