android - 使用 FragmentActivity 永远不会显示选项菜单

标签 android google-maps

我正在尝试编写一个应用程序来使用 Google map V2 显示预定义的位置。只是因为我必须兼容旧设备,所以我使用了一些兼容性库。

我的问题是菜单选项从未被调用。代码如下:

    package jv.android.getpointlib;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

import jv.android.getpoint.R;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.graphics.Color;
import jv.android.utils.Message;

import com.google.android.gms.maps.SupportMapFragment;

//public class PointViewV2 extends FragmentActivity {
public class PointViewV2 extends FragmentActivity {

    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pointviewv2);

        map = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();

        Intent intent = getIntent();
        Double lat = 0D;
        Double lon = 0D;
        double[] lats = null;
        double[] lons = null;
        String nome = "";

        if (intent != null)
        {
            Bundle params = intent.getExtras();

            if (params != null) {
                lat = (Double) params.getDouble("latitude");
                lon = (Double) params.getDouble("longitude");
                lats = (double[]) params.getDoubleArray("lats");
                lons = (double[]) params.getDoubleArray("lons");
                nome = params.getString("nome");
            }
        }

        try {
            if (lats == null || lons == null || lats.length == 0 || lons.length == 0) {
                final LatLng point = new LatLng(lat, lon);        
                map.addMarker(new MarkerOptions().position(point).title(nome).icon(BitmapDescriptorFactory.fromResource(R.drawable.waypoint2)));                
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 5));
                map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
            } else {
                map.addMarker(new MarkerOptions().position(new LatLng(lats[0], lons[0])).title(nome).icon(BitmapDescriptorFactory.fromResource(R.drawable.waypoint2)));                
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lats[0], lons[0]), 5));
                map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

                PolylineOptions path = new PolylineOptions();
                for (int i = 0; i < lats.length; i++) {
                    path.add(new LatLng(lats[i], lons[i]));
                }

                path.color(Color.BLUE);
                @SuppressWarnings("unused")
                Polyline line = map.addPolyline(path);
            }
        }
        catch (Exception e) {
            Message.showMessage(PointViewV2.this, getString(R.string.aviso), getString(R.string.avErroAbrindoMaps), R.drawable.exclamation);
        } /**/

        ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).setHasOptionsMenu(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu  menu) {     
        getMenuInflater().inflate(R.menu.point_view_v2, menu);
        return true;
/*      //Log.i("MYINFO", "I'm in");    
        MenuItem mi0 = menu.add(Menu.NONE, 0, 0, getString(R.string.mnVerMapa));
        mi0.setIcon(R.drawable.mapview);

        MenuItem mi1 = menu.add(Menu.NONE, 1, 1, getString(R.string.mnVerSatelite));
        mi1.setIcon(R.drawable.satelliteview);

        MenuItem mi2 = menu.add(Menu.NONE, 2, 2, getString(R.string.mnVerHibrido));
        mi2.setIcon(R.drawable.hybridview);

        MenuItem mi3 = menu.add(Menu.NONE, 3, 3, getString(R.string.mnVerTerreno));
        mi3.setIcon(R.drawable.terrainview);

        return true; /**/
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.mnMap) {
            map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        } else if (item.getItemId() == R.id.mnSatelite) {
            map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        } else if (item.getItemId() == R.id.mnHybrid) {
            map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        } else if (item.getItemId() == R.id.mnHybrid) {
            map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        }

        return true;
    }    

}

我的 XML 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          class="com.google.android.gms.maps.SupportMapFragment"/>

</LinearLayout>

和菜单选项:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/mnMap"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/mapview"
        android:title="@string/mnVerMapa"/>
    <item
        android:id="@+id/mnSatelite"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/satelliteview"
        android:title="@string/mnVerSatelite"/>
    <item
        android:id="@+id/mnHybrid"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/hybridview"
        android:title="@string/mnVerHibrido"/>
    <item
        android:id="@+id/mnTerrain"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/terrainview"
        android:title="@string/mnVerTerreno"/>

</menu>

我可以看到 map ,但缺少选项菜单:(

最佳答案

尝试将 onCreateOptionsMenu 中的 return 语句从 return true 更改为 return super.onCreatOptionsMenu(menu)。您应该更改 onOptionsItemSelected 的最后一个返回语句以也返回 super。

关于android - 使用 FragmentActivity 永远不会显示选项菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20622892/

相关文章:

android - 安全关闭数据库

android - CoordinatorLayout 内的水平 RecyclerView

android - AChartEngine - Axis 的线宽

android - 如何检测触摸触摸屏的形状

android - Lat Lng 值中的 NumberFormatException

java - 有没有一种方法可以从 JSON 文件获取结果?

android - 当用户停止使用录音机说话时自动录音并停止

javascript - 谷歌地图 API : undefined marker title

ios - 是否有与 mkmapsnapshotter 等效的 Google map ?

javascript - "The Google Maps Embed API must be used in an iframe."