简体   繁体   中英

onOptionsItemSelected is never called

I'm following this tutorial , and I'm writing a simple Activity .

...

UPDATE : After your tips I've edited my app, but the method onOptionsItemSelected() is still not called when I select an item from the popup menu. The popup menu is inflated when the user click on an item from the action bar.

The activity:

public class Map extends AppCompatActivity implements
    GoogleMap.OnMapLongClickListener, GoogleMap.OnMarkerClickListener,
    GoogleMap.OnMapClickListener, OnMapReadyCallback,
    ResourceState.OnResourceStateChangeListener {

...

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Map.menu = menu;
    getMenuInflater().inflate(R.menu.map_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        ...

        case R.id.menu_item_map_type:
            // Here I show the popup
            PopupMenu popup = new PopupMenu(this, findViewById(R.id.menu_item_map_type));
            popup.getMenuInflater().inflate(R.menu.map_popup, popup.getMenu());
            popup.show();
            break;

        case R.id.menu_item_satellite:
            // here the method onOptionsItemSelected is not directly called
            break;

        case R.id.menu_item_street:
            // here the method onOptionsItemSelected is not directly called
            break;
    }

    return super.onOptionsItemSelected(item);
}

public void callOnOptionsItemSelected(MenuItem item) {
    onOptionsItemSelected(item);
}

...

}

The primary menu:

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <item android:id="@+id/menu_item_send"
          android:icon="@drawable/ic_send"
          android:title="send"
          app:showAsAction="ifRoom"/>

    <item android:id="@+id/menu_item_location"
          android:icon="@drawable/ic_location_on"
          android:title="my position"
          app:showAsAction="ifRoom"/>

    <item android:id="@+id/menu_item_map_type"
          android:icon="@drawable/ic_map"
          android:title="map view"
          app:showAsAction="ifRoom"/>

</menu>

The popup menu:

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >

    <item
        android:id="@+id/menu_item_satellite"
        android:title="satellite"
        android:checkable="true"
        android:onClick="callOnOptionsItemSelected"/>

    <item
        android:id="@+id/menu_item_street"
        android:title="street"
        android:checkable="true"
        android:onClick="callOnOptionsItemSelected" />

</menu>

My temporary solution is defining the method callOnOptionsItemSelected() , wich calls the method onOptionsItemSelected() . By the way, I'd like to know what I'm missing.

(Another issue related to the popup is that the method setChecked() of the class MenuItem , is not working.)

I don't know why you add

android:id="@+id/layout_map_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" 

in your menu.xml file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM