如何使用谷歌地图';s V2 MapView for Android是否显示操作栏?

如何使用谷歌地图';s V2 MapView for Android是否显示操作栏?,android,google-maps,android-fragments,android-mapview,Android,Google Maps,Android Fragments,Android Mapview,我试图在谷歌地图上方显示操作栏,但我意识到一旦谷歌地图打开,它就不会使用我的应用程序,而是谷歌的应用程序,因此标题栏消失了 我知道使用MapView可以实现这一点,但我不知道如何从使用片段过渡到MapView,因为我得到的是NullPointerException 以下是AndroidManifest.xml: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.andro

我试图在谷歌地图上方显示操作栏,但我意识到一旦谷歌地图打开,它就不会使用我的应用程序,而是谷歌的应用程序,因此标题栏消失了

我知道使用MapView可以实现这一点,但我不知道如何从使用片段过渡到MapView,因为我得到的是
NullPointerException

以下是AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.test" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapActivity"
            android:label="@string/title_activity_map" >
        </activity>

    </application>

</manifest>
日志:

08-21 01:29:20.593  30698-30698/org.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{org./org.test.MapActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at 
org.test.MapActivity.setUpMapIfNeeded(MapActivity.java:183)
                at 
org.test.MapActivity.onCreate(MapActivity.java:77)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
                at android.app.ActivityThread.access$1500(ActivityThread.java:117)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:130)
                at android.app.ActivityThread.main(ActivityThread.java:3683)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:507)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
                at dalvik.system.NativeStart.main(Native Method)

谢谢。

请将setContentView(R.layout.activity\u地图视图)更改为setContentView(R.layout.activity\u地图)

因为这里

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

您没有膨胀包含此片段的版面。

请将setContentView(R.layout.activity\u映射视图)更改为setContentView(R.layout.activity\u映射)

因为这里

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

您没有膨胀包含此片段的布局。

这里有一个主要问题,不要在
活动中使用
com.google.android.gms.maps.MapView
,而是在
活动中包含
MapFragment
,如下所示:

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

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context="org.test.MapActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    map:cameraTargetLat="35.25215"
    map:cameraTargetLng="-112.24659"
    map:uiTiltGestures="true"
    map:uiZoomGestures="true"
    map:uiZoomControls="true" />

</LinearLayout>


或者使用
FrameLayout
并将其替换为
SupportMapFragment
,如果不起作用,请告诉我。

这里有一个主要问题,不要在
activity\u MapView.xml
中使用
com.google.android.gms.maps.MapView
,而是在
activity\u MapView.xml
中包含
MapFragment
,如下所示:

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

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context="org.test.MapActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    map:cameraTargetLat="35.25215"
    map:cameraTargetLng="-112.24659"
    map:uiTiltGestures="true"
    map:uiZoomGestures="true"
    map:uiZoomControls="true" />

</LinearLayout>
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        map = fm.getMap();
        if (map != null) {
       //Do some thing here

          }

或者使用
FrameLayout
并将其替换为
SupportMapFragment
,如果不起作用,请告诉我

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

        map = fm.getMap();
        if (map != null) {
       //Do some thing here

          }
映射声明应在if条件之外


映射声明应该在if条件之外

在build.gradle中定义依赖项

compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.google.android.gms:play-services:7.8.0'
要获取标题,请从AppCompatActivity扩展您的活动,并使用
setTitle()

要使用MapView,您不需要在Resume中进行任何检查。
只需在onCreate中通过异步方法获取地图。
setUpMap
方法将在map就绪时调用

public class MapActivity extends AppCompatActivity {
    private GoogleMap mMap;
    private MapView mapView;
    private Marker marker;

    private static final LatLng ONE = new LatLng(32.882216, -117.222028);
    private static final LatLng TWO = new LatLng(32.872000, -117.232004);
    private static final LatLng THREE = new LatLng(32.880252, -117.233034);
    private static final LatLng FOUR = new LatLng(32.885200, -117.226003);

    private ArrayList<LatLng> coords = new ArrayList<LatLng>();
    private static final int POINTS = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mapview);
        setTitle("My Map");
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                setUpMap(googleMap);
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
    private void setUpMap(GoogleMap map) {
        mMap = map;

        coords.add(ONE);
        coords.add(TWO);
        coords.add(THREE);
        coords.add(FOUR);
        for (int i = 0; i < POINTS; i++) {
            mMap.addMarker(new MarkerOptions()
                    .position(coords.get(i))
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
        }
    }
}
公共类MapActivity扩展了AppCompatActivity{
私有谷歌地图;
私有地图视图;
专用标记;
专用静态最终车床1=新车床(32.882216,-117.222028);
私人静态最终车床2=新车床(32.872000,-117.232004);
专用静态最终车床三个=新车床(32.880252,-117.233034);
专用静态最终车床四个=新车床(32.885200,-117.226003);
private ArrayList coords=new ArrayList();
专用静态最终整数点=4;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapview);
setTitle(“我的地图”);
mapView=(mapView)findViewById(R.id.mapView);
onCreate(savedInstanceState);
getMapAsync(新的OnMapReadyCallback(){
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
设置图(谷歌地图);
}
});
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
mapView.onDestroy();
}
@凌驾
受保护的void onResume(){
super.onResume();
onResume();
}
@凌驾
受保护的void onPause(){
super.onPause();
onPause();
}
@凌驾
public void onLowMemory(){
super.onLowMemory();
onLowMemory();
}
@凌驾
SaveInstanceState上受保护的无效(束超出状态){
super.onSaveInstanceState(超出状态);
mapView.onSaveInstanceState(outState);
}
私有void设置图(谷歌地图){
mMap=map;
增加(一);
增加(两个);
增加(三);
增加(四);
对于(int i=0;i
在build.gradle中定义依赖项

compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.google.android.gms:play-services:7.8.0'
要获取标题,请从AppCompatActivity扩展您的活动,并使用
setTitle()

要使用MapView,您不需要在Resume中进行任何检查。
只需在onCreate中通过异步方法获取地图。
setUpMap
方法将在map就绪时调用

public class MapActivity extends AppCompatActivity {
    private GoogleMap mMap;
    private MapView mapView;
    private Marker marker;

    private static final LatLng ONE = new LatLng(32.882216, -117.222028);
    private static final LatLng TWO = new LatLng(32.872000, -117.232004);
    private static final LatLng THREE = new LatLng(32.880252, -117.233034);
    private static final LatLng FOUR = new LatLng(32.885200, -117.226003);

    private ArrayList<LatLng> coords = new ArrayList<LatLng>();
    private static final int POINTS = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mapview);
        setTitle("My Map");
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                setUpMap(googleMap);
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
    private void setUpMap(GoogleMap map) {
        mMap = map;

        coords.add(ONE);
        coords.add(TWO);
        coords.add(THREE);
        coords.add(FOUR);
        for (int i = 0; i < POINTS; i++) {
            mMap.addMarker(new MarkerOptions()
                    .position(coords.get(i))
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
        }
    }
}
公共类MapActivity扩展了AppCompatActivity{
私有谷歌地图;
私有地图视图;
专用标记;
专用静态最终车床1=新车床(32.882216,-117.222028);
私人静态最终车床2=新车床(32.872000,-117.232004);
专用静态最终车床三个=新车床(32.880252,-117.233034);
专用静态最终车床四个=新车床(32.885200,-117.226003);
private ArrayList coords=new ArrayList();
专用静态最终整数点=4;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapview);
setTitle(“我的地图”);
mapView=(mapView)findViewById(R.id.mapView);
onCreate(savedInstanceState);
getMapAsync(新的OnMapReadyCallback(){
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
设置图(谷歌地图);
}
});
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
mapView.onDestroy();
}
@凌驾
受保护的void onResume(){
super.onResume();
onResume();
}
@凌驾
受保护的void onPause(){
super.onPause();
onPause();
}
@凌驾
public void onLowMemory(){
super.onLowMemory();