问题得到最后的已知位置

时间:2013-11-05 17:51:20

标签: android google-maps-android-api-2 locationmanager

我在两部手机上测试我的应用程序(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);
}

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

1 个答案:

答案 0 :(得分:1)

  

此代码有问题吗?

您没有检查myLoc是否为nullgetLastKnownLocation()不必返回非null值。