Getting long and lat returns a nullpointexception in android map v2 -
i tried following tutorial google here. believe followed every instruction written still nullpointexception when try long , lat coordinates.
it works fine if press target icon in map redirects me current location. when try programmatically, returns nullpointexception. tried looking in stackoverflow similar situations mine far none of them works.
if can provide me sample working source file or code can test myself appreciated. below source code in case missed something. in advance.
manifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="sample.map.activity" android:versioncode="1" android:versionname="1.0" > <supports-screens android:largescreens="true" android:normalscreens="true" android:smallscreens="false" android:xlargescreens="true" /> <uses-feature android:glesversion="0x00020000" android:required="true" /> <uses-permission android:name="android.permission.camera" /> <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" /> <uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-sdk android:minsdkversion="11" android:targetsdkversion="19" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <uses-library android:name="com.google.android.maps" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name="sample.map.activity" android:configchanges="orientation" android:label="@string/app_name" android:screenorientation="portrait" > <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="aizasyadqod8bhch40coyn19c6ds0uhyfkx25sa" /> </application> </manifest>
map activity class
import android.location.criteria; import android.location.location; import android.location.locationmanager; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.menu; import android.view.menuitem; import android.widget.toast; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.mapfragment; import com.google.android.gms.maps.model.latlng; public class mapactivity extends actionbaractivity { googlemap map; location location; latlng mylocation; locationmanager lm; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_map); lm = (locationmanager) getsystemservice(location_service); criteria criteria = new criteria(); string provider = lm.getbestprovider(criteria, true); map = ((mapfragment) getfragmentmanager().findfragmentbyid( r.id.map)).getmap(); map.setmylocationenabled(true); location = lm.getlastknownlocation(provider); double mlat = location.getlatitude(); double mlong = location.getlongitude(); toast.maketext(getapplicationcontext(), mlat+" "+mlong, toast.length_long).show(); map.animatecamera(cameraupdatefactory.newlatlngzoom(mylocation, 13)); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.map, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
here stack trace
05-18 22:45:44.043: e/androidruntime(1220): fatal exception: main 05-18 22:45:44.043: e/androidruntime(1220): process: sample.map.activity, pid: 1220 05-18 22:45:44.043: e/androidruntime(1220): java.lang.runtimeexception: unable start activity componentinfo{sample.map.activity/sample.map.activity.mapactivity}: java.lang.nullpointerexception 05-18 22:45:44.043: e/androidruntime(1220): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 05-18 22:45:44.043: e/androidruntime(1220): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 05-18 22:45:44.043: e/androidruntime(1220): @ android.app.activitythread.access$800(activitythread.java:135) 05-18 22:45:44.043: e/androidruntime(1220): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 05-18 22:45:44.043: e/androidruntime(1220): @ android.os.handler.dispatchmessage(handler.java:102) 05-18 22:45:44.043: e/androidruntime(1220): @ android.os.looper.loop(looper.java:136) 05-18 22:45:44.043: e/androidruntime(1220): @ android.app.activitythread.main(activitythread.java:5017) 05-18 22:45:44.043: e/androidruntime(1220): @ java.lang.reflect.method.invokenative(native method) 05-18 22:45:44.043: e/androidruntime(1220): @ java.lang.reflect.method.invoke(method.java:515) 05-18 22:45:44.043: e/androidruntime(1220): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 05-18 22:45:44.043: e/androidruntime(1220): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 05-18 22:45:44.043: e/androidruntime(1220): @ dalvik.system.nativestart.main(native method) 05-18 22:45:44.043: e/androidruntime(1220): caused by: java.lang.nullpointerexception 05-18 22:45:44.043: e/androidruntime(1220): @ sample.map.activity.mapactivity.oncreate(mapactivity.java:39) 05-18 22:45:44.043: e/androidruntime(1220): @ android.app.activity.performcreate(activity.java:5231) 05-18 22:45:44.043: e/androidruntime(1220): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 05-18 22:45:44.043: e/androidruntime(1220): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 05-18 23:04:59.383: w/activitythread(1354): classloader.loadclass: class loader returned thread.getcontextclassloader() may fail processes host multiple applications. should explicitly specify context class loader. example: thread.setcontextclassloader(getclass().getclassloader()); 05-18 23:04:59.813: i/process(1354): sending signal. pid: 1354 sig: 9 05-18 23:05:01.123: d/dalvikvm(1401): gc_for_alloc freed 89k, 6% free 2960k/3128k, paused 26ms, total 28ms 05-18 23:05:01.123: i/dalvikvm-heap(1401): grow heap (frag case) 3.569mb 635812-byte allocation 05-18 23:05:01.163: d/dalvikvm(1401): gc_for_alloc freed 2k, 5% free 3578k/3752k, paused 32ms, total 32ms 05-18 23:05:01.253: w/visite dates(1401): reselected 0 tab name unposted 05-18 23:05:01.463: d/(1401): hostconnection::get() new host connection established 0xb82b7f80, tid 1401 05-18 23:05:01.513: w/egl_emulation(1401): eglsurfaceattrib not implemented 05-18 23:05:01.523: d/openglrenderer(1401): enabling debug mode 0 05-18 23:51:25.343: w/egl_emulation(1401): eglsurfaceattrib not implemented 05-18 23:54:27.943: w/egl_emulation(1401): eglsurfaceattrib not implemented
here xml file of layout.
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.google.android.gms.maps.mapfragment"/>
i dont know have done in xml file,but xml file must this:
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.mapfragment" android:layout_width="match_parent" android:layout_height="match_parent"/> </relativelayout>
also add following,
< meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" > < /meta-data>
in manifest file below first meta-data tag(where have placed google api key).
also havent used camera code have written animate camera.my code:
cameraposition cameraposition = new cameraposition.builder().target( new latlng(your_lat, your_long)).zoom(12).build(); map.animatecamera(cameraupdatefactory.newcameraposition(cameraposition));
Comments
Post a Comment