5

I'm learning android and following a simple camera app tutorial. There's a snippet of code that I've reproduced but I am getting an error on it and I'm not sure why.

The tutorial I'm using is,

http://iwearshorts.com/blog/android-development-102/

The snippet of code is:

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

In my project, I am getting an error with R.menu.main. More specifically, the word menu in 'R.menu.main' cannot be resolved or is not a field.

I don't see anything in the tutorial that I've missed that would cause this. The only thing I can think of is that it's related to using a different version of Android. I'm not sure how I would check this though.

I appreciate any advice, thank you.

Talen Kylon
  • 1,908
  • 7
  • 32
  • 60

7 Answers7

10

add the menu.xml file inside /res/menu/ folder, this is an example:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="my menu Item!"
        app:showAsAction="never"/>

</menu>
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
1

You can change the file name R.menu.main to R.menu.menu_main by default

Add the camera item in src/main/res/menu/menu_main.xmlsuch that

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

    <item android:id="@+id/action_camera"
        android:title="Camera"
        android:showAsAction="always"/>

</menu>
bjiang
  • 6,068
  • 2
  • 22
  • 35
1

on your res/ directory right click, and choose new >> android resource file . and then name your menu as you like but make resource type is menu and android studio will create it for you and then in the file you should find this

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

and then add your items as you like

<item 
//item attributes>
</item>
Mina Shaker
  • 383
  • 3
  • 12
1

Try this ...

public boolean OnCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
Community
  • 1
  • 1
Jiten Patel
  • 123
  • 9
1

You are getting this error because the android studio still does not have build the gradle with the new files that you have created.

So, you search for Sync project with Gradle Files and then after that restart android studio.

Then you will be able to see that it has recognized the menu directory in res directory.

Hope it helps.

0

Your app is looking for a file called main.xml located in the folder src/main/res/menu/.

This file is used to create the buttons inside the top ActionBar. If you don't need theses buttons, juste delete the line and only return false.

pdegand59
  • 12,776
  • 8
  • 51
  • 58
0
public boolean OnCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Safwan
  • 1