I am trying to enable/disable a refresh button when certain things happen in my app, but I get a null pointer exception that I can't figure out. I am setting a boolean addingRefresh or removingRefresh to true depending on the situation and then calling invalidateOptionsMenu() to enable or disable the button, however the menu item is returned null. I have searched the internet for why this may be but can't find anything.
Code for onCreateOptionsMenu() (called when invalidateOptionsMenu() is called)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (addingRefresh) {
//below line as well as other similar line cause exceptions
menu.findItem(R.id.action_refresh).setEnabled(true);
addingRefresh = false;
} else if (removingRefresh) {
menu.findItem(R.id.action_refresh).setEnabled(false);
removingRefresh = false;
} else if (addingLoading) {
}
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Thanks for any help!
Here is some cleaned up code for what you are trying to accomplish:
private MenuItem mMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
mMenuItem = menu.findItem(R.id.action_refresh)
return true;
}
private void setMenuItemEnabled(boolean enabled) {
mMenuItem.setEnabled(enabled);
}
Hope that helps!
Just for clearing the doubt.
You should use
menu.findItem(R.id.menuId);
after inflating the menu
for me cause of issue was wrong import:
android.widget.SearchView
instead
androidx.appcompat.widget.SearchView
and take care about namespaces
app:actionViewClass
Related
I have a very annoying problem, I was testing on Samsung (s3) device and every thing was working just fine, then I used LG device with OS 4.0 and When I try to open my application I got stackoverflowerror in the following method
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actions_menu, menu);
}
My base activity extending ActionBarActivity from android.support.v7
can any one help please ?
EDIT 1
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actions_menu, menu);
MenuItem editItem = menu.findItem(R.id.action_edit);
MenuItem saveItem = menu.findItem(R.id.action_save);
MenuItem deleteItem = menu.findItem(R.id.action_delete);
editItem.setVisible(isEditEnable());
saveItem.setVisible(isSaveEnable());
deleteItem.setVisible(isDeleteEnable());
invalidateOptionsMenu();
return true;
}
the exception occurred in getMenuInflater().inflate(R.menu.actions_menu, menu);
Looks like you calling return onCreateOptionsMenu(); after inflation the menu.
instead just use return true;
update:
You're calling invalidateOptionsMenu(); which i think will call onCreateOptionsMenu() again.
Remove it and change your menu in onPrepareOptionMenu(), there is no need to call invalidateOptionsMenu()
I have the below code and I can't understand why my code doesn't show up menu. If the activity containing data, condition_true = true otherwise condition_true = false. I can clearly see the control going to menutag and the condition is true too. Even I am getting Inflated string too in log. But still menu doesn't show up. I have seen lot of posts on these like link 1, link2 etc., but that didn't solve my problem. Can someone please help me on this issue?
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); //Clear view of previous menu
MenuInflater inflater = getSupportMenuInflater();
Log.d("menutag", condition_true);
if(condition_true)
{
inflater.inflate(R.layout.menu, menu);
Log.d("menutag","Inflated");
}
return super.onPrepareOptionsMenu(menu); // replaced this with true, but no use.
}
You have to add the options attributes in xml.. Otherwise you can do it programmatically like this:
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.yourmenuxmlfilename, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId){
case R.id.item1:
// what you want to do with first button
break;
case .....
break;
}
return true;
}
Maybe you're overriding onKeyDown and it always returns true? This can cause the problem, because it will catch the menu button pressed event too.
I've met a serious problem. I want to add the MenuItem after the user successfully logged in.
At the beginning I use the following method to make it available.
The following code doesn't work now , I mentioned it as to illustrate my idea.
Private Menu mymenu;
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
mymenu=menu;
return super.onCreateOptionsMenu(menu);
}
public void process(){
MenuInflater inflater=this.getMenuInflater();
inflater.inflate(R.menu.my_menu,mymenu);
}
It seems this idea worked as I remembered.But after doing something I couldn't remember , It doesn't work!
After the debug , I find it's because mymenu=null .
I guessed there is a way to set mymenu=menu
Can anyone help?
The problem you could be having with that code is calling the super twice.
To do what you want, you could override onPrepareOptionMenu and set the visibility to hidden on all menu items you don't want until your state is such that you want to "add" it.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem menuItem = menu.findItem(R.id.<id of menuitem>);
menuItem.setVisible(<condition>);
return true;
}
and just use standard code in onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.<id of menu with all items you'll need>, menu);
return true;
}
There is a method, onPrepareOptionsMenu(Menu menu) method which you could override and dynamically change the menus.
hello i would like to present a different menu in my activity depending on some condition. i tried like this.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
if (specialMode)
{
inflater.inflate(R.menu.menuA, menu);
}
else
{
inflater.inflate(R.menu.menuB, menu);
}
return true;
}
however, i always get the same menu, no matter what the value of specialMode is.
onCreateOptionsMenu is called only once ( upon first creation), try using onPrepareOptionsMenu method it get called every time menu is shown.
I need to enable a MenuItem when a previous screen (Activity) returns.
I tried this code:
...
((MenuItem)findViewById(R.id.menu_how)).setEnabled(true);
...
but a null pointer exception is launched.
BTW, the menu_how is set to false in xml; and the code is part of onActivityResult(int requestCode, int resultCode, Intent data) call.
Try using menu.findItem(R.id.menu_how) in onCreateOptionsMenu and save a reference for later use.
This should work fine with enabled, however, I've found that setting a menu item to invisible in the XML means you can't show/hide it programmatically.
I found something at the android dev site that might be helpful (look for the section "Changing menu items at runtime")
It said that the onCreateOptionsMenu() method fired only when the the menu for the activity is created, and it happens when this activity starts. So if you want to change the menu items after the menu/activity was created, you need to override the onPrepareOptionsMenu() method instead. search the link for full details.
EDIT:
Just made it and it's working fine. I'm using one boolean var per menuItem which represents if this item should be enabled or not. This is my code:
/*************************************Game Menu**************************************/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId())
{
case R.id.gm_new_game:
//newGame();
return true;
case R.id.gm_stand_up:
//some code when "gm_stand_up" button clicked..
return true;
case R.id.gm_forfeit:
//some code when "gm_forfeit" button clicked..
return true;
case R.id.gm_surrender:
//some code when "gm_surrender" button clicked..
return true;
case R.id.gm_exit_table:
exitTableCommand();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
menu.findItem(R.id.gm_forfeit).setEnabled(forfeitMenuButtonIsEnabled);
menu.findItem(R.id.gm_surrender).setEnabled(surrenderMenuButtonIsEnabled);
menu.findItem(R.id.gm_new_game).setEnabled(newGameMenuButtonIsEnabled);
menu.findItem(R.id.gm_stand_up).setEnabled(standUpMenuButtonIsEnabled);
return super.onPrepareOptionsMenu(menu);
}
where are you calling this? (Sorry, didn't read carefully) I think you need to call it after the menu is inflated (usually in OnCreateOptionsMenu). To do this, you can set a variable to true when the other Activity returns, then do ((MenuItem)findViewById(R.id.menu_how)).setEnabled(mMyBooleanField) in OnCreateOptionsMenu after the call to inflater.inflate.
Edit: To accomplish this in code, it might look something like this:
At the top of the class (along with all the other class members):
Boolean mEnableMenuItem = false;
In OnCreateOptionsMenu:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_main, menu);
((MenuItem)findViewById(R.id.menu_how)).setEnabled(mEnableMenuItem );
In OnActivityResult:
mEnableMenuItem = true;
Keep a reference to Menu in your activity:
private Menu mMenu;
Then:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_note, menu);
mMenu = menu;
return true;
}
Now, to access menu items anywhere in your activity use similar code to this:
mMenu.findItem(R.id.menu_how).setVisible(false);
or
mMenu.findItem(R.id.menu_how).setEnabled(true);