• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

cannot resolve method 'onOptionsItemSelected(android.view.MenuItem)'

You are probably going to be asked to post code and stack trace so the developers here can lend a hand. Please use [code][/code] tags to make things more readable. Good luck!!!:)

[code]Line 1
Line 2
Line 3
...
...
Line 999[/code]

Code:
Line 1
Line 2
Line 3
...
...
Line 999
 
Hello. I am trying to Create a navigation drawer from the tutorial on https://developer.android.com/training/implementing-navigation/nav-drawer#java , but I have an error. When I type "return super.onOptionsItemSelected(item);" I get an error on "onOptionsItemSelected", it says 'cannot resolve method 'onOptionsItemSelected(android.view.MenuItem)'.
I am using Android Studio and the code is:
Code:
package com.urbannet.user2.receitassaudaveis;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.support.v7.widget.Toolbar;

import com.urbannet.user2.receitassaudaveis.Carnes.ReceitasCarnesActivity;
import com.urbannet.user2.receitassaudaveis.Peixe.ReceitasPeixeActivity;
import com.urbannet.user2.receitassaudaveis.Sopas.ReceitasSopasActivity;
import com.urbannet.user2.receitassaudaveis.afrodesiacas.AfrodesiacasActivity;
import com.urbannet.user2.receitassaudaveis.bolo.ReceitasBolosActivity;
import com.urbannet.user2.receitassaudaveis.outras.OutrasReceitasActivity;
import com.urbannet.user2.receitassaudaveis.vegetarianas.ReceitasVegetarianasActivity;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);

        mDrawerLayout = findViewById(R.id.drawer_layout);

        mDrawerLayout.addDrawerListener(
                new DrawerLayout.DrawerListener() {
                    @Override
                    public void onDrawerSlide(View drawerView, float slideOffset) {

                    }

                    @Override
                    public void onDrawerOpened(View drawerView) {

                    }

                    @Override
                    public void onDrawerClosed(View drawerView) {

                    }

                    @Override
                    public void onDrawerStateChanged(int newState) {

                    }
                }
        );

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                public boolean onNavigationItemSelected(MenuItem menuItem){
                    menuItem.setChecked(true);
                    mDrawerLayout.closeDrawers();
                    return true;
                }
                public boolean onOptionsItemSelected(MenuItem item){
                    switch (item.getItemId()) {
                        case android.R.id.home:
                            mDrawerLayout.openDrawer(GravityCompat.START);
                            return true;

                    }
                    return super.onOptionsItemSelected(item);
                }
            });
           }

    public void iniciarBolo(View view) {
        Intent intent =new Intent(this, ReceitasBolosActivity.class);
        startActivity(intent);
    }

    public void iniciarCarne(View view) {
        Intent intent = new Intent(this, ReceitasCarnesActivity.class);
        startActivity(intent);
    }

    public void iniciarOutras(View view) {
        Intent intent = new Intent(this, OutrasReceitasActivity.class);
        startActivity(intent);
    }

    public void iniciarPeixe(View view) {
        Intent intent = new Intent(this, ReceitasPeixeActivity.class);
        startActivity(intent);
    }

    public void iniciarSopas(View view) {
        Intent intent = new Intent(this, ReceitasSopasActivity.class);
        startActivity(intent);
    }

    public void iniciarVegetariana(View view) {
        Intent intent = new Intent(this, ReceitasVegetarianasActivity.class);
        startActivity(intent);
    }

    public void iniciarAfrodesiacas(View view) {
        Intent intent = new Intent(this, AfrodesiacasActivity.class);
        startActivity(intent);
    }

    public void iniciarUrbanNet(View view) {
        Intent intent = new Intent(this, UrbanNetActivity.class);
        startActivity(intent);
    }
}
 
Basically there is only one method you need to implement in the OnNavigationItemSelectedListener, and that is onNavigationItemSelected().

This method is called whenever you select an item from the navigation drawer. You will probably want to add some more code in this method to do something useful.
 
Back
Top Bottom