문제

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