"Expected resource menu type" when inflating MenuButton

So, I have this XML file in my layouts 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 as follows:

 @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 really works. I am just annoyed by this warning in Intellij, and I'm not quite sure that I am doing something wrong.

+5
source share
3 answers

Just move the xml file to

"Menu"

resource directory instead

"location"

Catalog. Then change the line

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

FROM

  inflater.inflate(R.menu.actionbar_buttons, menu); 
+13
source

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); } 
+3
source

In my case, I meant the resource using R.id.my_resource instead of R.layout.my_resource . Later it worked perfectly.
(The problem was in the layout instead of the menu)

0
source

Source: https://habr.com/ru/post/1200699/


All Articles