我在两部手机(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);
}
这段代码有问题吗?谢谢,加里