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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top