nullpointerexception - Android Location Manager returning NULL -


i have simple location manager works, when android device has been turned off , turned on, android location manager returns null when have requesting updates. aware getlastknownlocation can return null, believe handling in code. suggestions appreciated.

apparently lon = location.getlongitude(); crashing it.

locationmanager lm = (locationmanager) getsystemservice(context.location_service);         location location = lm.getlastknownlocation(locationmanager.gps_provider);         if (location == null)         {             // request location update!!             lm.requestlocationupdates(locationmanager.gps_provider, 0, 0, this);             lon = location.getlongitude();             lat = location.getlatitude();         }           mlocationmanager = (locationmanager) getsystemservice(context.location_service);          //get last known location         location = mlocationmanager.getlastknownlocation(locationmanager.gps_provider);         //update if not null         if (location != null)         {             lat = location.getlatitude();             lon = location.getlongitude();         }         //request update location manager can return null otherwise         else         {             mlocationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, this);             lat = location.getlatitude();             lon = location.getlongitude();         }     } 

the android location manager returns null when have requesting updates

correct. requesting updates requests android start trying find out device is. take while. in meantime, getlastknownlocation() can return null.

getlastknownlocation() useful if know location, if data not ready, can move on without it. if need location, , getlastknownlocation() returns null, wait use location until onlocationchanged() called. note onlocationchanged() may never called, there no requirement user have location providers enabled, , there no requirement device in position location fixes.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -