Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Development - getMenuInflater(R.menu.main, menu)

Tags:

android

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.

like image 991
Talen Kylon Avatar asked Dec 12 '22 02:12

Talen Kylon


2 Answers

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>
like image 142
Jorgesys Avatar answered Jan 12 '23 12:01

Jorgesys


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>
like image 28
bjiang Avatar answered Jan 12 '23 11:01

bjiang