0

I'm having trouble to highlight the right option in the navigation drawer and asked here in Stackoverflow about it. Now I'm having trouble with the answers I got. To be concrete, this is the error message I'm getting from Android Studio: 'Attempt to invoke virtual method 'android.view.Menu android.support.design.widget.NavigationView.getMenu()' on a null object reference' and it is referred to these lines of code.

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.

    navId = item.getItemId();
    clearChecked();
    navigationView.getMenu().findItem(navId).setChecked(true);

    if (navId == R.id.nav_home) {
        // Handle the action

    } else if (navId == R.id.nav_my_profile){

        Intent intent = new Intent(this, MyProfile.class);
        startActivity(intent);

    } else if (navId == R.id.nav_buy_puntos) {

    } else if (navId == R.id.nav_settings){

    } else if (navId == R.id.nav_share) {

    } else if (navId == R.id.nav_feedback) {

        Intent intent = new Intent(this, FeedbackActivity.class);
        startActivity(intent);

    } else if (navId == R.id.nav_help) {

    } else if (navId == R.id.nav_log_out){

        finish();
        BaseActivity.hideProgressDialog();

    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

private void clearChecked() {
    int size = navigationView.getMenu().size();
    for (int i = 0; i < size; i++) {
        navigationView.getMenu().getItem(i).setChecked(false);
    }
}
Nkuutz
  • 41
  • 4
  • 1
    are you sure you're setting the `navigationView` to your view before this method is called? – Can Poyrazoğlu Jun 12 '16 at 09:54
  • Show your `onCreate` method please. – Vucko Jun 12 '16 at 09:55
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Vucko Jun 12 '16 at 09:55
  • You can find the onCreate method on the following link: http://stackoverflow.com/questions/37727075/navigation-drawer-highlight-not-working-properly – Nkuutz Jun 12 '16 at 13:42

0 Answers0