192.168.04.5 2022-03-29 23:03 采纳率: 0%
浏览 204

AndroidStudio实现底部导航栏报错,怎么解决

问题遇到的现象和发生背景

在虚拟机上运行时出现闪退

问题相关代码,请勿粘贴截图
public class FunctionPage1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_function_page1);
        // 获取底部导航栏组件的容器
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigation);
        // 获取之前存在与导航栏组件之前的控件
        NavController navController ;
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
        navController = navHostFragment.getNavController();
        // 导航栏的配置
        AppBarConfiguration configuration = new AppBarConfiguration.Builder(bottomNavigationView.getMenu()).build();
        // 装配这些控件(报错出现在下面两行代码)
        NavigationUI.setupActionBarWithNavController(this,navController,configuration);
        NavigationUI.setupWithNavController(bottomNavigationView,navController);
    }
}

运行结果及报错内容
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.studyapplication/com.example.studyapplication.FunctionPage1}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference


我想要达到的结果

完美解决这个问题

  • 写回答

3条回答 默认 最新

  • 不会写代码的猴子 优质创作者: 操作系统技术领域 2022-03-30 09:31
    关注
    AppBarConfiguration configuration = new AppBarConfiguration.Builder(bottomNavigationView.getMenu()).build();
    

    你这里是不是缺少一个layout?setDrawerLayout

    评论

报告相同问题?

问题事件

  • 创建了问题 3月29日