使用osmdroid获取当前位置时出现空指针异常
Null Pointer Exception while getting current location using osmdroid
我正在使用 osmdroid API 获取当前位置并在该点显示叠加层,但出现以下错误。
错误是由String provider = mLocMgr.getBestProvider(criteria, true);
引起的,但我不知道是什么问题。
我的Activity有错误
public class LSLocationSender extends Activity implements LocationListener, MapViewConstants {
protected MapView mMapView;
protected IMapController mapController;
protected LocationManager mLocMgr;
protected ItemizedOverlay<OverlayItem> mMyLocationOverlay;
protected ResourceProxy mResourceProxy;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
setContentView(R.layout.ls_location_sender);
mMapView = (MapView) this.findViewById(R.id.mapview);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
mapController = this.mMapView.getController();
mapController.setZoom(15);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
String provider = mLocMgr.getBestProvider(criteria, true);
Location location = mLocMgr.getLastKnownLocation(provider);
GeoPoint mypoint = new GeoPoint(location.getLatitude(), location.getLongitude());
GeoPoint mypointicon = new GeoPoint(location.getLatitude()+1000, location.getLongitude()+1000);
mapController.setCenter(mypoint);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100, this);
ArrayList<OverlayItem> items=new ArrayList<OverlayItem>();
items.add(new OverlayItem("Here", "Sample Description", mypointicon));
this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(final int index,
final OverlayItem item) {
Toast.makeText(
LSLocationSender.this,
"Item '" + item, Toast.LENGTH_LONG).show();
return true; // We 'handled' this event.
}
@Override
public boolean onItemLongPress(final int index,
final OverlayItem item) {
Toast.makeText(
LSLocationSender.this,
"Item '" + item ,Toast.LENGTH_LONG).show();
return false;
}
}, mResourceProxy);
this.mMapView.getOverlays().add(this.mMyLocationOverlay);
mMapView.invalidate();
}
@Override
public void onLocationChanged(Location location) {
GeoPoint currLoc = new GeoPoint(location);
mapController.setCenter(currLoc);
mMapView.invalidate();
}
@Override
public void onProviderDisabled(String arg0) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
我的Logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.saathi/com.android.saathi.LSLocationSender}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.location.LocationManager.getBestProvider(android.location.Criteria, boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access0(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.location.LocationManager.getBestProvider(android.location.Criteria, boolean)' on a null object reference
at com.android.saathi.LSLocationSender.onCreate(LSLocationSender.java:53)
at android.app.Activity.performCreate(Activity.java:5953)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
你永远不会初始化 mLocMgr
你应该在使用它之前调用它。
mLocMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
关于你之前的代码:
String provider = mLocMgr.getBestProvider(criteria, true);
另外记住 AndroidManifest 中的权限,如果还没有添加的话:
<uses-permission android:name="ANDROID.PERMISSION.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
我正在使用 osmdroid API 获取当前位置并在该点显示叠加层,但出现以下错误。
错误是由String provider = mLocMgr.getBestProvider(criteria, true);
引起的,但我不知道是什么问题。
我的Activity有错误
public class LSLocationSender extends Activity implements LocationListener, MapViewConstants {
protected MapView mMapView;
protected IMapController mapController;
protected LocationManager mLocMgr;
protected ItemizedOverlay<OverlayItem> mMyLocationOverlay;
protected ResourceProxy mResourceProxy;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
setContentView(R.layout.ls_location_sender);
mMapView = (MapView) this.findViewById(R.id.mapview);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
mapController = this.mMapView.getController();
mapController.setZoom(15);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
String provider = mLocMgr.getBestProvider(criteria, true);
Location location = mLocMgr.getLastKnownLocation(provider);
GeoPoint mypoint = new GeoPoint(location.getLatitude(), location.getLongitude());
GeoPoint mypointicon = new GeoPoint(location.getLatitude()+1000, location.getLongitude()+1000);
mapController.setCenter(mypoint);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100, this);
ArrayList<OverlayItem> items=new ArrayList<OverlayItem>();
items.add(new OverlayItem("Here", "Sample Description", mypointicon));
this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(final int index,
final OverlayItem item) {
Toast.makeText(
LSLocationSender.this,
"Item '" + item, Toast.LENGTH_LONG).show();
return true; // We 'handled' this event.
}
@Override
public boolean onItemLongPress(final int index,
final OverlayItem item) {
Toast.makeText(
LSLocationSender.this,
"Item '" + item ,Toast.LENGTH_LONG).show();
return false;
}
}, mResourceProxy);
this.mMapView.getOverlays().add(this.mMyLocationOverlay);
mMapView.invalidate();
}
@Override
public void onLocationChanged(Location location) {
GeoPoint currLoc = new GeoPoint(location);
mapController.setCenter(currLoc);
mMapView.invalidate();
}
@Override
public void onProviderDisabled(String arg0) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
我的Logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.saathi/com.android.saathi.LSLocationSender}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.location.LocationManager.getBestProvider(android.location.Criteria, boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access0(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.location.LocationManager.getBestProvider(android.location.Criteria, boolean)' on a null object reference
at com.android.saathi.LSLocationSender.onCreate(LSLocationSender.java:53)
at android.app.Activity.performCreate(Activity.java:5953)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
你永远不会初始化 mLocMgr
你应该在使用它之前调用它。
mLocMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
关于你之前的代码:
String provider = mLocMgr.getBestProvider(criteria, true);
另外记住 AndroidManifest 中的权限,如果还没有添加的话:
<uses-permission android:name="ANDROID.PERMISSION.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>