Reputation: 1219
I get a NullPointerException everytime i run the code. I have the Internet and GPS permissions (ACCESS_FINE_LOCATION, ACCESS_MOCK_LOCATION, & ACCESS_COARSE_LOCATION) enabled in the code. I have tried with both emulator and phone, both fails. It is not a problem of unavailable GPS or Internet, AFAIK.
My code snippet:-
@Override
public void onCreate (Bundle savedInstanceState)
{
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mapView.setStreetView(false);
mapView.setSatellite(false);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
mc = mapView.getController();
// String coordinates[] = {"51.708945", "8.745928"}; //THIS WORKS IF ENABLED
// double lat = Double.parseDouble(coordinates[0]);
// double lng = Double.parseDouble(coordinates[1]);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double lat = location.getLatitude(); //NULLPOINTER EXCEPTION THROWN HERE
double lng = location.getLongitude();
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
} catch (Exception e) {}
}
It throws NullPointer Exception when i am trying to get the latitude or the longitude. How do I avoid it? The locationManager object does not throw any exception on creation (using getSystemService method) nor does it give any exception when requesting location updates. I can check if the object is null before using it, but i dont understand why does it null at that point...is it worth putting a sleep? would that be the right way of programming? I dont understand why it doesnt work when i try to get the longitude/latitude. Am i doing something wrong?
Problems addressed here, or here or here could not solve my issue. Thank you.
Upvotes: 0
Views: 1340
Reputation: 25060
I would say that mostly likely problem is that the LocationManager doesn't have a last known location so it's returning null. In that case you'll need to wait for a location update to get the position.
-= Update =- Just do this:
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
}
Since this is an internal class it has access to all of your Activity members.
Upvotes: 1