finditem() doesn't find menu, stuck with NullPointerException - android

I'm stuck while changing some properties on my options menu at onCreateOptionsMenu(). It seems like findItem() returns null, even though I'm pretty sure that the reference to the menu item is correct. My code looks as follows:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_profile, menu);
MenuItem leftie = menu.findItem(R.id.menu_profile);
leftie.setIcon(R.drawable.ic_menu_mapmode);
leftie.setTitle(R.string.back_map);
leftie.setIntent(authIntent);
return true;
}
I really don't know what can be wrong there. Thanks in advance :)
EDIT: I forgot to include the actual problem.

You can mention title and image for that menu item in XML.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/newsItem"
android:icon="#drawable/news_tab"
android:title="#string/menu_news"/>
<item
android:id="#+id/dryiceItem"
android:icon="#drawable/dryice_tab"
android:title="#string/menu_dryice"/>
</menu>
and can set intent on menuItem like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.newsItem:
// start News activity
//write your intent here.
break;
case R.id.dryiceItem:
//start another activity
//write your intent here.
break;
}
}

I figured it out. The String that references to the menu index, R.menu.activity_profile was the wrong path so it was inflating an empty menu. I changed the string to R.menu.layout and now it works as expected.
System.out.println(menu.size());
MenuItem leftie = menu.findItem(R.id.menu_profile);
System.out.println(leftie);
leftie.setIcon(R.drawable.ic_menu_mapmode);
leftie.setTitle(R.string.back_map);
leftie.setIntent(authIntent);

I've also had this happen when I had a submenu that wasn't properly nested in item tags, like
<item />
<menu>
</menu>
or
<item >
<menu>
</menu>
<item>

Typecast your findItem statement with MenuItem
MenuItem leftie = (MenuItem) menu.findItem(R.id.menu_profile);

Related

how to set switch inside item list of toolbar in android?

I am trying to set switch in item list beside title in toolbar. Is there is any way to set the switch beside the title in item list, as I have shown in Image. Can any one help me to find out the solution.
Thank you.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/extend"
android:title="#string/clear_once_a_week"/>
</menu>
I think the below solution might work for you. So give it a try and go through the defined steps.
Step 1: Create a layout with a switch (for ex.switch_layout):
RelativeLayout
-> Add a Switch/SwitchCompat inside your parent layout
Step 2: In your activity override onCreateOptionsMenu as below:
#Override
public boolean onCreateOptionsMenu(#NonNull Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
MenuItem item = menu.findItem(R.id.id_of_your_menu_item);
item.setActionView(R.layout.switch_layout); // Set your custom layout here
SwitchCompat mSwitch = item.getActionView().findViewById(R.id.id_of_your_switch);
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
}
});
return true;
}
Or you can set an action view in your menu item directly instead of
adding it programmatically. Choose the best way according to your requirement.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/myswitch"
android:title=""
android:showAsAction="always"
android:actionLayout="#layout/switch_layout"
/>

Android onCreateOptionsMenu not called when hitting the menu button

I have the problem that my application doesn't show the options menu when I hit the menu button. Debugging shows that the onCreateOptionsMenu(Menu menu) method is not called after hitting the menu button. I have another application with the same code for the menu and there it works. So now my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.app_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
Intent intent = new Intent(this, OptionsActivity.class);
startActivityForResult(intent, 1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In res -> menu -> app_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/options" android:title="#string/options" />
</menu>
I have no idea why the onCreateOptionsMenu is not called after hitting the menu button. I hope you guys can help me.
Edit: I'm not using Fragments and the onCreateOptionsMenu is really never called. Not at the start of the app and not when i'm hitting the menu button on my device.
Not sure from your post that you're using Fragments. If so, you must set menu options on by
setHasOptionMenu(true);
call this method from Fragment's onCreate() and the options menu will then be shown.
Try to add these item attributes in your app_menu.xml file:
<item
android:id="#+id/options"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/options"/>
You must declare a string for options in Strings.xml
It will call when app starts for the first time not after menu item selected.
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (item.getItemId() == R.id.menuitem_id) {
}
return super.onMenuItemSelected(featureId, item);
}
this method will be called after selection
In Manifest file just set target versions as below:
android:minSdkVersion="8" android:targetSdkVersion="10"

getActionView() of my MenuItem return null

I just would like to tweak the View of an ActionBar MenuItem by code.
Unfortunately, it seems that getActionView always return null!
My code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = this.getSupportMenuInflater();
inflater.inflate(R.menu.folder, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onPrepareOptionsMenu(final Menu menu) {
MenuItem menuFolder = menu.findItem(R.id.menu_folder);
Log.i("", "* onPrepareOptionsMenu *" + menuFolder);
Log.i("", "* getActionView *" + menuFolder.getActionView());
Log is:
01-11 22:13:42.884: I/(7893): * onPrepareOptionsMenu
*com.actionbarsherlock.internal.view.menu.MenuItemWrapper#41401ac8
01-11 22:13:42.884: I/(7893): * getActionView *null
Thank a lot for any help
Edit:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_folder"
android:icon="#drawable/ic_menu_archive"
android:showAsAction="always"/>
</menu>
you should use
app:actionLayout="#layout/menu_actionbar_basket"
thats the trick
if you use
android:actionLayout="#layout/menu_actionbar_basket"
you would always get null exception in default toolbar.
getActionView() only works if there's a custom actionView from setActionView.
For me the solution that worked is to use app namespace instead of android.
app:actionViewClass="android.support.v7.widget.SearchView"
Don't forget to declare it:
xmlns:app="http://schemas.android.com/apk/res-auto"
First Solution
This happen may be you not set actionLayout in Menu file
so set your actionLayout in menu file
app:actionLayout="#layout/your_action_layout"
Second Solution
and the second solution is
from
android:actionLayout="#layout/your_action_layout"
to
app:actionLayout="#layout/your_action_layout"
If your debug build is working without any issues and issue is only with release build then this may be because of proguard configuration. If you have enabled proguard in your application then add below line to proguard-rules.pro
-keep class android.support.v7.widget.SearchView { *; }
in order to get the MenuItem View i use:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.someMenuItem:
View v = findViewById(R.id.someMenuItem);
doSomethingWithView(v);
break;
}
return super.onOptionsItemSelected(item);
}

Android Submenu, how to go back to main menu?

I use an xml to for my ContextMenu, which is like :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/Ordermenu" android:title="Order">
<menu android:id="#+id/OrderBySubMenu">
<item android:id="#+id/OrderByASC" android:title="Order ASC" />
<item android:id="#+id/OrderByDESC" android:title="Order DESC" />
<item android:id="#+id/Cancel" android:title="Cancel" />
</menu>
</item>
<item android:id="#+id/ActionAmenu" android:title="Action A"/>
</menu>
I use following code to display the menu, in my onCreateContextMenu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.my menu, menu);
I manage option click with following code :
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Displaymenu:
//do stuff
return true;
case R.id.OrderByASC:
//do stuff
return true;
case R.id.OrderByDESC:
//do stuff
return true;
default :
return(super.onOptionsItemSelected(item));
}
Starting the Context Menu it display Two options:
Order
Action A
Clicking on Order show a submenu :
Order ASC
Order DESC
Cancel
Now, If the user click on cancel (or click on the hardware back button), no action is specified, so it call super.onOptionsItemSelected(item) which go back to my main activity.
How can I manage to go back to the main menu in such case? i.e. diplay the initial :
Order
Action A
I tried this long ago but i think you will have to override onPrepareOptionsMenu as well to get this to work. This is called before it shows, and you will have to put flags here on what items to show for the user.
Try something like this:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
// Clear the previous layout
menu.clear();
if(showMainMenu)
{
// Add main menu items..
menu.add(0, R.id.ordermenu, 0, "True");
}
else
{
// Add sub-menu items
menu.add(0, R.id.ordermenuASC, 0, "True");
}
return super.onPrepareOptionsMenu(menu);
}
So when user clicks a main menu item, change the boolean flag a redo the process.
Finally, it worked only by adding :
case R.id.Cancel:
openContextMenu(findViewById(selected_view_id));
return true;
in public boolean onContextItemSelected(MenuItem item)
selected_view_id is stored by
selected_view_id=v.getId();
in onCreateContextMenu
Hope it will help others.

Android Actionbar with Tabs

I am very new to Android and Java programming. What I am trying to do is to get Actionbar with couple of tabs (11 in final). I have managed to install ActionBar Sherlock but then I am stuck with adding tabs to actionbar. From what I read it looks that also I would need to add separate Fragment for each tab. Is there a simple solution how to make that work.
You don't need to have a fragment per tab in the action bar. It should just be as simple as defining your menu in xml, then in your activity initialising the action bar with your menu.
For instance your menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/my_menu_item"
android:showAsAction="always|withText"
android:title="Tab name here"/>
</menu>
Then in your activity override onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
Then you'll need to override onOptionItemSelected to respond to when a tab is pressed:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.my_menu_item:
//do something
}
return super.onOptionsItemSelected(item);
}

Categories

Resources