【问题标题】:ActionBarSherlock Options menu not displayed on Google Maps fragment谷歌地图片段上未显示 ActionBarSherlock 选项菜单
【发布时间】:2014-01-10 01:12:43
【问题描述】:

我正在使用 ActionBarSherlock 在 Android 2.2 上显示碎片标签 我遇到的问题是我无法在 Google 地图片段上显示我的自定义 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);
    }

}

它没有出现是因为它在一个片段中吗?有什么方法可以在谷歌地图片段中启用我自己的自定义菜单? 欢迎大家回答和讨论, 如果您需要查看我的任何处理片段或 .xml 文件的适配器类,请告诉我。

【问题讨论】:

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


    【解决方案1】:

    您可以尝试将菜单保存在 menu 文件夹中,然后像这样扩展该菜单

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

    【讨论】:

    • 我不能使用超级用户。在我的片段中,也许这就是我当前菜单没有显示的原因,因为我使用的是return onCreateOptionsMenu(menu); 而不是return super.onCreateOptionsMenu(menu); 如何制作超级。在片段中工作的方法?
    • @user3135324 这个方法是我从我项目的一个片段中挑选出来的,它运行良好。
    • 如果我使用 super 我收到错误 The method onCreateOptionsMenu(Menu, MenuInflater) in the type SherlockFragment is not applicable for the arguments (Menu)
    • 你导入了错误的Menu,Menu Inflater尝试导入sherlock包的menu和menu inflater。
    • 当我导入 Menu The method onCreateOptionsMenu(Menu, MenuInflater) in the type SherlockFragment is not applicable for the arguments (Menu) 时它仍然会给出相同的错误,如果我从 Sherlock 导入两者似乎并不重要
    猜你喜欢
    • 2015-12-11
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    相关资源
    最近更新 更多