使用osmdroid获取当前位置时出现空指针异常

时间:2015-03-09 17:37:32

标签: android api maps openstreetmap osmdroid

我使用osmdroid API来获取当前位置并在该点显示叠加层,但是我收到以下错误。

错误是由String provider = mLocMgr.getBestProvider(criteria, true);造成的,但我不知道错误。

我的活动有错误

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.access$800(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)

1 个答案:

答案 0 :(得分:4)

在使用之前,您永远不会初始化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"/>