GetMap() return null on Android with Google Maps API v2

匿名 (未验证) 提交于 2019-12-03 08:52:47

问题:

I have some problems with Google Maps API V2. I have tried many tutorials and searched many answers (include stack overflow) but all I have found was not work. I can draw a map with xml tag <fragment>. No problem, it works. But when I try to get the map in MainActivity.java getMap() return null and I don't know why.

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:

Add your API KEY

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

and you can write like below

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

my layout file was like

<?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> 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!