Android OnCreateOptionsMenu - android

I've made menu's before, so I'm not a complete rookie, but I've encountered a problem where Eclipse tells me I have to remove #Override, but obviously if I do then my code won't run to inflate the menu. the code is:
#Override
public boolean OnCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.map_menu, menu);
return super.onCreateOptionsMenu(menu);
}

The "on" in "OnCreateOptionsMenu" should not be capitalized

You can add overridden methods in eclipse by going Source > Override/implement methods and then check the methods you want to override,it helps with the spelling and all.

Related

What is the menuInflater in Android Studio?

I've been studying Kotlin and Android development, and studying the code samples in Android Studio, I've encountered this block:
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
I know that first you have to instantiate a variable for the inflater to use the inflate() method, but there is no menuInflater variable in this code. Looking at it, I see that it is similar to getMenuInflater() but I don't get how this is possible. I looked at the documentation and I have not found any explanation. Is menuInflater a variable, class, method?
Thank you in advance for the answer.
menuInflater is an object you can take from activity since activity and Menu class which you take menuInflater
shares common classes in the inheritance hierarchy. You can get the menuInflater object in activity, and it gives you chance
to get a menu file to your activity and use it there, Inflaters are mostly used to get some layouts and use it in another
layout e.g LayoutInflater, MenuInflater
MenuInflater is class, You can find detailed Documentation Here
Also you can find a simple example for MenuInflater Here
It's Kotlin's syntactic sugar. getXxx() Java method can be invoked as if it was a Kotlin xxx property.
Reference: https://kotlinlang.org/docs/java-interop.html#getters-and-setters

menuItem.getActionView() == null in API 15

I'm supporting down to API 15 in my app, and I'm getting a crash for those users when I try to get the searchView from my menu.
Below is my code:
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuItem = menu.getItem(0);
searchView = (SearchView)menuItem.getActionView();
searchView.setIconifiedByDefault(false);
searchView.setQuery("", true);
menuItem.expandActionView();
}
I'm getting a NullPointerException on this line:
searchView.setIconifiedByDefault(false);
because the searchView is null. This works perfectly fine on devices at API 16 and above. Has anyone run into this issue before?
While inflating a layout usually causes an immediate crash if there is a problem, inflating a menu resource does not. If there is some problem, a stack trace is logged, but otherwise the exception is handled, and so execution continues. It is only some time later that we realize that something did not work, when things break later on.
Custom action bar items (actionLayout, actionViewClass, actionProvider) are especially prone to this. If there is some problem loading any of those -- such as the actionViewClass not implementing the proper constructor -- we only find out about it when we try to retrieve the custom item and get null back. The solution is to rummage through LogCat and look for the stack trace associated with the handled exception, to see what really went wrong.
In an API level-dependent case, like this one, the most likely scenario would be where initialization of custom action item refers to a method that does not exist on the older version of Android, and therefore fails.
I also had just the same problem as you and came to this stackoverflow question. With some struggle, I have found the heart of this problem and a solution.
In API15, during app's initialization, only onCreateOptionsMenu is called but onPrepareOptionsMenu is not.
In API16 and later, onPrepareOptionsMenu is called right after onCreateOptionsMenu.
So my solution is to call onPrepareOptionsMenu at the ending point of onCreateOptionsMenu:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
(...)
if (Build.VERSION.SDK_INT < 16) {
onPrepareOptionsMenu(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.
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);
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);
}
In my case, I was referring to the resource using R.id.my_resource instead of R.layout.my_resource. The later worked fine.
(The problem was with Layout instead of Menu)

How to access Menu using Menuinflater [duplicate]

This question already has answers here:
create new menuInflater or getMenuInflater() from activity?
(3 answers)
Closed 8 years ago.
I am new to android and would like to create menu using menu inflater. To access menu on android emulator, one could simply press the menu button and menu would get displayed. However on a real device, how can I access the menu using menu inflater?
It probably sounds silly but like I said before, new to android.
The app's min. api level is 8.
Thanks
Here is the working link for it :
http://androidcodejunction.wordpress.com/2012/07/25/ok/
Use this
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
MenuInflater exampleInflater = getActivity().getMenuInflater();
exampleInflater.inflate(R.menu.example_menu, menu);
}
The "example_menu" being the name off the xml you have in the "menu" folder. Also there are many duplicates of this question.

Error with onCreateOptionsMenu

Getting an error and not sure why.
I'm trying to get icons beside the icon of my app and the title of my app at the top of the screen.
I've tried to implement the onCreateOptionsMenu method however I'm getting this error.
'The method inflate(int, Menu) in the type MenuInflater is not applicable for the arguments (int, Menu).
This is the code I tried
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.testmenu, menu);
return true;
}
I've read that sometimes it can be related to the theme or the SDK version you're trying to target however I've checked both and they're fine. Version 11 is the min and I have the Holo.Light theme currently
Are you using ActionBarSherlock? If so, you have to use
getSupportMenuInflater().inflate(R.menu.testmenu, menu);

Categories

Resources