Android Google Map V2 - Find my location error -


i'm trying retrieve device's location faced several problems:

my logcat:

07-21 22:23:34.072: e/androidruntime(11634): fatal exception: main 07-21 22:23:34.072: e/androidruntime(11634): java.lang.runtimeexception: unable start activity componentinfo{com.test.projecttest/com.test.projecttest.mainactivity}: java.lang.nullpointerexception 07-21 22:23:34.072: e/androidruntime(11634):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2081) 07-21 22:23:34.072: e/androidruntime(11634):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2106) 07-21 22:23:34.072: e/androidruntime(11634):    @ android.app.activitythread.access$700(activitythread.java:134) 07-21 22:23:34.072: e/androidruntime(11634):    @ android.app.activitythread$h.handlemessage(activitythread.java:1217) 07-21 22:23:34.072: e/androidruntime(11634):    @ android.os.handler.dispatchmessage(handler.java:99) 07-21 22:23:34.072: e/androidruntime(11634):    @ android.os.looper.loop(looper.java:137) 07-21 22:23:34.072: e/androidruntime(11634):    @ android.app.activitythread.main(activitythread.java:4856) 07-21 22:23:34.072: e/androidruntime(11634):    @ java.lang.reflect.method.invokenative(native method) 07-21 22:23:34.072: e/androidruntime(11634):    @ java.lang.reflect.method.invoke(method.java:511) 07-21 22:23:34.072: e/androidruntime(11634):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1007) 07-21 22:23:34.072: e/androidruntime(11634):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:774) 07-21 22:23:34.072: e/androidruntime(11634):    @ dalvik.system.nativestart.main(native method) 07-21 22:23:34.072: e/androidruntime(11634): caused by: java.lang.nullpointerexception 07-21 22:23:34.072: e/androidruntime(11634):    @ com.test.projecttest.mainactivity.setupmap(mainactivity.java:82) 07-21 22:23:34.072: e/androidruntime(11634):    @ com.test.projecttest.mainactivity.oncreate(mainactivity.java:71) 07-21 22:23:34.072: e/androidruntime(11634):    @ android.app.activity.performcreate(activity.java:5047) 07-21 22:23:34.072: e/androidruntime(11634):    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1094) 07-21 22:23:34.072: e/androidruntime(11634):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2045) 07-21 22:23:34.072: e/androidruntime(11634):    ... 11 more 

my mainactivity.java code:

public class mainactivity extends fragmentactivity implements onclicklistener, locationlistener {  private googlemap map;  protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);         supportmapfragment fmap = (supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map);         map = fmap.getmap();         setupmap();         }       private void setupmap() {         map.setmylocationenabled(true);         locationmanager locationmanager = (locationmanager) getsystemservice(location_service);         criteria criteria = new criteria();         string provider = locationmanager.getbestprovider(criteria, true);         location mylocation = locationmanager.getlastknownlocation(provider);         map.setmaptype(map.map_type_hybrid);         double latitude = mylocation.getlatitude();         double longitude = mylocation.getlongitude();         latlng latlng = new latlng(latitude, longitude);         map.movecamera(cameraupdatefactory.newlatlng(latlng));         map.animatecamera(cameraupdatefactory.zoomto(20));         map.addmarker(new markeroptions().position(new latlng(latitude, longitude)).title("i'm here"));      } } 

i tried use codes on google.developer.com confusing can't run understand of them , got errors when tried run them. should solve problem? comments appreciated. :)

the call locationmanager.getlastknownlocation(provider) not blocking. there no guarantee location immediately. said location object null.

to prevent should use locationlistener onlocationchanged() callback sure valid location. callback returns location available.

another fact if app never had last location not one. first have call requestforlocationupdates().

edit: in docs described in detail.

you can use this:

requestlocationupdates (provider, mintimeforupdates, mindistanceforupdates, yourlocationlistener);


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -