user1271277
user1271277

Reputation: 3

Android::menu cannot be resolved or is not a field

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings1, menu);
return true;
}

I need help. i got error at "R.menu" which the error message "menu cannot be resolved or is not a field"..Thanks

Upvotes: 0

Views: 1593

Answers (3)

Tony Chan
Tony Chan

Reputation: 8097

Check your imports. If you imported some kind of .R file from some other project (maybe a library project) like import com.someotherpackage.R; you need to delete that line, then clean your project.

Right now it's probably referencing the wrong .R file, or you have a typo in a name somewhere.

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

you are using Preference xml as an menu reference that's why reciving this error so make a menu.xml or add as:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
this.getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}

and you can add dynamically as:

@Override
        public boolean onCreateOptionsMenu(Menu menu) {
            menu.add(Menu.NONE, Menu.FIRST + 1, 5, "??").setIcon(
                    android.R.drawable.ic_menu_delete);
            menu.add(Menu.NONE, Menu.FIRST + 2, 2, "??").setIcon(
                    android.R.drawable.ic_menu_edit);
            return true; 
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case Menu.FIRST + 1:
               startActivity(new Intent(getBaseContext(), SettingActivity.class));
                break;
            case Menu.FIRST + 2:
               Toast.makeText(getBaseContext(), "Menu Clicked", Toast.LENGTH_SHORT).show();
                break;
            }
        return false;
    }

Upvotes: 0

Jokahero
Jokahero

Reputation: 1074

If there is error in your layout the R file is not generated so it is normal to get this error.

You need to correct the errors in your layout first, then cleaning, and R will be resolved.

Upvotes: 1

Related Questions