Pregunta

I test my apps on two phones (1) an LG Optima running 2.3.7 and (2) a galaxy S3 running 4.1.2. The following code always works on the LG and always fails with a nullPointerException on the Galaxy..

try { //sometimes getting the last known location gets a nullpointerexception
    String locationProvider = LocationManager.GPS_PROVIDER;
    locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Location myLoc = locMgr.getLastKnownLocation(locationProvider);
    CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(myLoc.getLatitude(), myLoc.getLongitude()));
    map.animateCamera(center);
    DebugLog.debugLog("Instantiated new Location listener.  Moved camera to: " + myLoc.getLatitude() + myLoc.getLongitude() , false);

} catch (Exception e) {
    DebugLog.debugLog("Exception getting lastknownlocation - doing without" + e, false);
}

Is there something wrong with this code? Thanks, Gary

¿Fue útil?

Solución

Is there something wrong with this code?

You are not checking to see if myLoc is null. getLastKnownLocation() does not have to return a non-null value.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top