// 我学习了关于BottomNavigationView的android studio,但我有这样的问题...请帮帮我:(// 引起:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.BottomNavigationView.setOnNavigationItemSelectedListener(android.support.design.widget.BottomNavigationView$OnNavigationItemSelectedListener)' 在一个空对象引用上//错误日志public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation); bottomNav.setOnNavigationItemSelectedListener(navListener);}private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { Fragment selectedFragment = null; switch (menuItem.getItemId()) { case R.id.nav_home: selectedFragment = new HomeFragment(); break; case R.id.nav_search: selectedFragment = new SearchFragment(); break; case R.id.nav_filter: selectedFragment = new PesananFragment(); break; case R.id.nav_notifikasi: selectedFragment = new NotificationFragment(); break; case R.id.nav_akun: selectedFragment = new AccountFragment(); break; } assert selectedFragment != null; getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit(); return true; } };}
添加回答
举报
0/150
提交
取消