java - GetMap() return null on Android with Google Maps API v2 -
i have problems google maps api v2. have tried many tutorials , searched many answers (include stack overflow) have found not work. can draw map xml tag <fragment>
. no problem, works. when try map in mainactivity.java
getmap()
return null , 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 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"/> <!-- following 2 permissions not required use google maps android api v2, 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>
add api key
<meta-data android:name="com.google.android.maps.v2.api_key" android:value="aizasya_ywwmn2h21nvvzuiqcpqsipzoalaix7z" /> // place api key
and can write below
android.support.v4.app.fragmentmanager myfm = getsupportfragmentmanager(); final supportmapfragment mymapf = (supportmapfragment) myfm.findfragmentbyid(r.id.mapview); googlemap = mymapf.getmap();
my layout file 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>
Comments
Post a Comment