5

I keep receiving this warning in my code and I am starting to worry as I am close to my deadline of completion. I have a DrawerLayout in my main activity, using Android Studio's own navigation drawer activity but had to make a slight change to the code as the DrawerLayout setDrawerListener is now deprecated.

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

    //Firebase reference to main application URL and 'today's' matches
    Firebase mainRef = new Firebase(cons.getFirebaseUrl());

    //DrawerLayout settings for the navigation drawer
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    //Retrieve the navigation drawer and set it's listener for menu item presses
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

I am receiving the warning on drawer.addDrawerListener(toggle) and navigationView.setNavigationItemSelectedListener(this)

Further down in my code, I am also receiving it in this method:

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) // This line gets the warning {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

Thanks in advance for anyone who may have an idea as to what is causing it. I have searched this issue but I couldn't find an example on the post deprecated method.

Charlie
  • 276
  • 3
  • 13

1 Answers1

1

Make sure you have used ActionBarDrawerToggle in support-v7-appcompat.

Replace the code drawerLayout.setDrawerListener(actionBarDrawerToggle);

by drawerLayout.addDrawerListener(actionBarDrawerToggle);

Lastly, check for the null drawer and null navigationview like this:

        DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        assert drawer != null;
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        assert navigationView != null;
        navigationView.setNavigationItemSelectedListener(this);
Hartok
  • 2,147
  • 20
  • 37