Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Expected resource of type menu" when inflating MenuButton

So I've got this XML file in my layout directory called "actionbar_buttons.xml":

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_height="match_parent"
      android:layout_width="match_parent">
     <item android:id="@+id/action_settings"
           android:title="Settings">
     </item>
     <item android:id="@+id/action_settings2"
           android:title="fooo">
     </item>
 </menu>

In my Fragment class I call the inflate method like so:

@Override
public void onCreateOptionsMenu(
        Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu( menu, inflater );
    inflater.inflate(R.layout.actionbar_buttons, menu);
}

Now Intellij complains and tells me:

Expected resource of type menu less... (Ctrl+F1) 
Reports two types of problems:
Supplying the wrong type of resource identifier. For example, when calling      Resources.getString(int id), you should be passing R.string.something, not R.drawable.something.
Passing the wrong constant to a method which expects one of a specific set of constants. For example, when calling View#setLayoutDirection, the parameter must be android.view.View.LAYOUT_DIRECTION_LTR or android.view.View.LAYOUT_DIRECTION_RTL.

The code actually does work. I am just annoyed with that warning in Intellij and I'm not entirely sure if I am doing something wrong.

like image 597
stephanlindauer Avatar asked Aug 19 '14 20:08

stephanlindauer


2 Answers

Just move the xml file to the

"menu"

resource directory instead of the

"layout"

directory. Then change the line

inflater.inflate(R.layout.actionbar_buttons, menu);

With

 inflater.inflate(R.menu.actionbar_buttons, menu);
like image 60
Ahmed Ghonim Avatar answered Oct 14 '22 21:10

Ahmed Ghonim


I think it should be like:

@Override
public void onCreateOptionsMenu(
        Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu( menu, inflater );
    inflater.inflate(R.menu.actionbar_buttons, menu);
}
like image 25
user3487063 Avatar answered Oct 14 '22 22:10

user3487063