0

我在两部手机(1)运行 2.3.7 的 LG Optima 和(2)运行 4.1.2 的 Galaxy S3 上测试我的应用程序。以下代码始终在 LG 上运行,并且在 Galaxy 上始终失败并出现 nullPointerException。

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);
}

这段代码有问题吗?谢谢,加里

4

1 回答 1

1

这段代码有问题吗?

您没有检查是否myLocnull. getLastKnownLocation()不必返回非null值。

于 2013-11-05T17:56:12.900 回答