问题遇到的现象和发生背景
在虚拟机上运行时出现闪退
问题相关代码,请勿粘贴截图
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
我想要达到的结果
完美解决这个问题