2

I am developping an app with a menu. The menu contains 2 items, a map and a settings property This is the menu syntax:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />
    <item android:id="+@id/action_maps"
        android:title="@string/action_map"
        app:showAsAction="never"/>
</menu>

I use the onOptionsItemSelected(MenuItem item) method in the MainActivity to handle this:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent intent = new Intent(this, PreferenceActivity.class);
            startActivity(intent);
            return true;
        }

        if(id == R.id.action_maps){
            openPreferredLocationMap();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

Now is the problem that Android recognizes action_settings in the method but he doesn't recognize action_maps. Why?

Jonas
  • 769
  • 1
  • 8
  • 37

3 Answers3

5

This is wrong

<item android:id="+@id/action_maps"
    android:title="@string/action_map"
    app:showAsAction="never"/>

Try this

<item android:id="@+id/action_maps"
    android:title="@string/action_map"
    app:showAsAction="never"/>

The problem is + symbol should be after @ not before

TechArcSri
  • 1,982
  • 1
  • 13
  • 20
0

I removed android:orderInCategory="100" and the Id got recognized again

dave o grady
  • 788
  • 8
  • 12
-1

You accidentally switched "+" and "@" in the action_map item of your menu

Templerschaf
  • 142
  • 1
  • 8
  • When I started looking into this there was no answer, he just found the answer quicker. But cheers for reminding me why I rarely actually post anything here. – Templerschaf Aug 12 '15 at 11:47