GetMap()在Android上使用Google Maps API v2返回null

时间:2014-06-27 08:09:56

标签: java android xml api google-maps

我在使用Google Maps API V2时遇到了一些问题。我已经尝试了很多教程并搜索了很多答案(包括堆栈溢出),但我找到的所有都没有用。我可以用xml标签<fragment>绘制地图。没问题,它有效。但是,当我尝试在MainActivity.java getMap()中获取地图时,返回null并且我不知道原因。

MainActivity.java

package com.example.testmaps;

import android.app.Activity;
import android.os.Bundle;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {
 private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    //HERE mMap IS NULL
    mMap.addMarker(new MarkerOptions()
            .position(new LatLng(10, 10))
            .title("Hello world"));
    }
}

activity_main.xml中

<RelativeLayout 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"
    tools:context=".MainActivity" >

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

</RelativeLayout> 

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testmaps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <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"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testmaps.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="my_key" />
        <meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    </application>

</manifest>

1 个答案:

答案 0 :(得分:5)

添加API密钥

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyA_YwwmN2h21nVVzuiQcpQsipZoAlaix7Z" />
     // place your API KEY

你可以写下面的

 android.support.v4.app.FragmentManager myFM = getSupportFragmentManager();
 final SupportMapFragment myMAPF = (SupportMapFragment) myFM.findFragmentById(R.id.mapView);
 googleMap = myMAPF.getMap();

我的布局文件就像

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_horizontal_margin" >

<fragment
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true" />

<Button
    android:padding="5dp"
    android:layout_margin="10dp"
    android:background="@drawable/button"
    android:id="@+id/refreshMap"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="@string/button_refresh_map" />

 </RelativeLayout>