[英]onOptionsItemSelected is never called
我正在關注本教程 ,並且正在編寫一個簡單的Activity
。
...
更新 :根據您的提示,我已經編輯了我的應用程序,但是當我從彈出菜單中選擇一個項目時,仍未調用onOptionsItemSelected()
方法。 當用戶單擊操作欄上的項目時,彈出菜單會膨脹。
活動:
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);
}
...
}
主菜單:
<?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>
彈出菜單:
<?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>
我的臨時解決方案是定義方法callOnOptionsItemSelected()
,並調用方法onOptionsItemSelected()
。 順便說一句,我想知道我所缺少的。
(與彈出窗口有關的另一個問題是類MenuItem
setChecked()
方法不起作用。)
我不知道你為什么加
android:id="@+id/layout_map_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
在您的menu.xml
文件中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.