0
我使用ActionBarSherlock在Android 2.2上顯示碎片選項卡 我遇到的問題是我無法在Google地圖片段上顯示我的自定義選項菜單。我設置了setHasOptionsMenu(true);在onCreate方法中,但它仍然不出現。ActionBarSherlock選項菜單不顯示在Google地圖片段
這是我的谷歌地圖fragment3.java
public class Fragment3 extends SherlockFragment {
private MapView mMapView;
private int group1Id = 1;
int homeId = Menu.FIRST;
int profileId = Menu.FIRST +1;
int searchId = Menu.FIRST +2;
int dealsId = Menu.FIRST +3;
int helpId = Menu.FIRST +4;
int contactusId = Menu.FIRST +5;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment3, container, false);
mMapView = (MapView) view.findViewById(R.id.mapview);
// inflat and return the layout
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
GoogleMap googleMap = mMapView.getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(54.902815,23.9500459) , 16.0f));
// googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
}
/*
* Using a mapview in a fragment requires you to 'route'
* the lifecycle events of the fragment to the mapview
*/
@Override
public void onResume() {
super.onResume();
if (null != mMapView)
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
if (null != mMapView)
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
if (null != mMapView)
mMapView.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (null != mMapView)
mMapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
if (null != mMapView)
mMapView.onLowMemory();
}
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(group1Id, homeId, homeId, "").setIcon(R.drawable.action_about);
menu.add(group1Id, profileId, profileId, "").setIcon(R.drawable.action_about);
menu.add(group1Id, searchId, searchId, "").setIcon(R.drawable.action_about);
menu.add(group1Id, dealsId, dealsId, "").setIcon(R.drawable.action_about);
menu.add(group1Id, helpId, helpId, "").setIcon(R.drawable.action_about);
menu.add(group1Id, contactusId, contactusId, "").setIcon(R.drawable.action_about);
return onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
........
case 6:
//code here
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
它不會出現,因爲它是在一個片段?有什麼方法可以在谷歌地圖片段中啓用我自己的自定義菜單嗎? 歡迎所有的答案和討論, 讓我知道,如果你需要查看我的任何處理片段或.xml文件的適配器類。
我不能用戶超。在我的片段中,也許這就是爲什麼我的當前菜單沒有顯示的原因,因爲我正在使用 'return onCreateOptionsMenu(menu);'而不是 'return super.onCreateOptionsMenu(menu); ' 我該怎麼做超級。方法工作在一個片段? – user3135324
@ user3135324這個方法我從我的一個項目的片段中挑選出來,它運行良好。 –
如果我使用超級我得到錯誤'在SherlockFragment類型中的方法onCreateOptionsMenu(Menu,MenuInflater)不適用於您導入了錯誤'Menu'的參數(Menu)' – user3135324