在Android中使用MapController的setCenter方法时出现空指针异常

时间:2011-05-20 12:44:08

标签: java android android-maps

我正在使用此代码设置地图的中心。

mapController.setCenter(new GeoPoint((int)(location.getLatitude()*1E6), (int) (location.getLongitude()*1E6)));

其中location被定义为

locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);

provider = locationManager.getBestProvider(criteria, true);

Location location = locationManager.getLastKnownLocation(provider);

我使用此代码获得空指针异常。我做错了什么?

1 个答案:

答案 0 :(得分:1)

  

如果当前禁用了提供程序,则返回null。

您可能从locationManager.getLastKnownLocation(provider)获得null响应;

http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29