android - my map app stops unexpectedly -
i'm new android. i'm working on app using google map. after several test, have same problem : app stops unexpectedly.
i present code.
the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.map_project" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="10" android:targetsdkversion="21" /> <permission android:name="com.map_project.permission.maps_receive" android:protectionlevel="signature" /> <uses-feature android:glesversion="0x00020000" android:required="true" /> <uses-permission android:name="com.map_project.permission.maps_receive" /> <uses-permission android:name="android.permission.internet" /> <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/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".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="mykeyxxx" /> </application> </manifest>
the map_activity.xml:
<?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" > <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.supportmapfragment" /> </relativelayout>
and mainactivity:
package com.map_project; import android.os.bundle; import android.support.v4.app.fragmentactivity; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.latlng; public class mainactivity extends fragmentactivity { public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.map_activity); googlemap map = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map)).getmap(); latlng sydney = new latlng(-33.867, 151.206); map.setmylocationenabled(true); map.movecamera(cameraupdatefactory.newlatlngzoom(sydney, 13)); } }
when run application on phone, message: unfortunately, myapp has stopped. if can me, nice because begin despair.
firstly know testing on emulator or android mobile ?
if on emulator
, ensure create google api based emulator. this emulator can used test google map , other google play service integration.
1) ok if use fragmentmanager instead of supportfragmentmanager , import same !
2)try declaring static final latlng sydney = new latlng(-33,151);
before oncreate method
3)instead of extending fragmentactivity can extend activity
your code looks good! can recheck these steps regarding api key:
4)make sure have created api key in googleapi console , activated google maps android api.(google console -> services -> on them)
use try-catch trace errors
try { if (googlemap == null) { googlemap = ((mapfragment) getfragmentmanager(). findfragmentbyid(r.id.map)).getmap(); } googlemap.setmaptype(googlemap.map_type_hybrid); marker syd = googlemap.addmarker(new markeroptions(). position(sydney).title("its sydney")); } catch (exception e) { e.printstacktrace(); }
Comments
Post a Comment