尝试在空对象引用上调用虚拟方法DrawerLayout.addDrawerListener(androidx.drawerlayout.widget.DrawerLayout $ DrawerListener)'

有人可以帮我吗,我完全不知道是什么原因导致了此错误!谢谢!

LogCat

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.drawerlayout.widget.DrawerLayout.addDrawerListener(androidx.drawerlayout.widget.DrawerLayout$DrawerListener)' on a null object reference
    at com.musicapp.android.musicapp.MainActivity.onCreate(MainActivity.java:71)

码:

ActionBarDrawerToggle actionBarDrawerToggle;
DrawerLayout drawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
    bottomNav.setOnNavigationItemSelectedListener(navListener);

    drawerLayout = findViewById(R.id.drawerLayout);

    //Action Bar drawer Toggle
70        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolBarMainAc,R.string.openDrawer,R.string.closeDrawer);
71        drawerLayout.addDrawerListener(actionBarDrawerToggle);
72        actionBarDrawerToggle.syncState();
73        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
评论