You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BottomNavigationView在Fragment切换后消失并引发崩溃问题求助

问题: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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 10:15:28