ActionBarSherlock选项菜单未显示在Google地图片段上

时间:2014-01-09 17:09:10

标签: android google-maps android-fragments actionbarsherlock android-optionsmenu

我正在使用ActionBarSherlock在Android 2.2上显示碎片标签 我遇到的问题是我无法在Google Maps片段上显示我的自定义OptionsMenu。我设置了setHasOptionsMenu(true);在onCreate方法中,但它仍然没有出现。

这是我的谷歌地图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);
    }

}

它不会出现,因为它在片段中吗?有什么办法可以在google地图片段中启用我自己的自定义菜单吗? 欢迎所有答案和讨论, 如果您需要查看处理片段的任何适配器类或.xml文件,请告诉我。

1 个答案:

答案 0 :(得分:0)

您可以尝试将您的菜单保存在menu文件夹中,并可以像这样对该菜单进行充气

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.main, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }