BottomNavigationView在Fragment切换后消失并引发崩溃问题求助
我实现了一个包含4个菜单项的BottomNavigationView,在HomeFragment的CardView上设置点击事件,点击后跳转到NotificationsFragment,但新Fragment加载后BottomNavigationView完全消失,点击返回按钮时因BottomNavigationView空引用触发崩溃,错误信息:
Attempt to invoke virtual method 'android.view.Menu com.google.android.material.bottomnavigation.BottomNavigationView.getMenu()' on a null object reference
相关布局与错误代码
BottomNavigationView的XML代码
<com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/nav_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" android:background="?android:attr/windowBackground" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/bottom_nav_menu" />
main_activity.xml布局(包含BottomNavigationView和NavHostFragment)
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/nav_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" android:background="?android:attr/windowBackground" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/bottom_nav_menu" /> <fragment android:id="@+id/nav_host_fragment_activity_main" android:name="androidx.navigation.fragment.NavHostFragment" class="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:layout_constraintBottom_toTopOf="@id/nav_view" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" app:navGraph="@navigation/mobile_navigation" /> </androidx.constraintlayout.widget.ConstraintLayout>
HomeFragment的错误点击事件代码
public class HomeFragment extends Fragment { private static final int AUTOCOMPLETE_REQUEST_CODE = 3; private static final String TAG = "HomeFragment"; private FragmentHomeBinding binding; @RequiresApi(api = Build.VERSION_CODES.N) @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); CardView card = getView().findViewById(R.id.card1); Log.e(TAG, "Found Card!"); card.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { getActivity().setContentView(R.layout.fragment_notifications); Log.e(TAG, "Starting Login Activity"); Fragment fragment = new NotificationsFragment(); getChildFragmentManager().beginTransaction() .setCustomAnimations(R.anim.slide_in,R.anim.slide_in) .replace(R.id.fragment_home, fragment) .addToBackStack("HomeFragment") .commit(); } }); } }
问题原因
错误核心在于点击事件中调用了getActivity().setContentView(R.layout.fragment_notifications),这会直接替换整个Activity的布局,原布局中的BottomNavigationView被销毁,后续操作自然触发空指针异常。同时使用getChildFragmentManager()手动管理Fragment事务,不符合Navigation组件的设计规范。
解决方案
使用Navigation组件提供的导航方法进行Fragment跳转,既能保持Activity布局结构不变,也能让BottomNavigationView正常显示:
// 获取当前Fragment实例 Fragment current = this; // 通过Navigation组件跳转至NotificationsFragment NavHostFragment.findNavController(current).navigate(R.id.navigation_notifications);
将HomeFragment中点击事件内的错误代码替换为上述代码即可解决问题。
内容的提问来源于stack exchange,提问作者James Gyps
相关产品推荐
相关产品推荐

