我在两部手机上测试我的应用程序(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);
}
这段代码有问题吗? 谢谢, 加里