Search code examples
javaandroidxmlmenumenuitem

Android doesn't recognize my id


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?


Solution

  • 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