Take the 2-minute tour ×

I am trying to get a user's location when a button is clicked.
I have the following code in my button onclick listener:

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
Location currentLocation = locationManager.getLastKnownLocation(bestProvider);
Log.e(tag,"reached till here");
location = Double.toString(currentLocation.getLatitude()) + " " + Double.toString(currentLocation.getLongitude());
Toast.makeText(getApplicationContext(), location, Toast.LENGTH_LONG);

I am getting the the output reached till here in logcat. After that the application stops and asks me to force close it. I initially did some searching and found out that getLatitude and getLongitude return double values. So I corrected the code. But still I am getting an error. What am I doing wrong?

EDIT : The logcat error:
Got RemoteException sending setActive(false) notification to pid 796 uid 10036
I think currentLocation is returning null

share|improve this question
can you debug and see if your object "currentLocation" is null? and also, what is the error you get in the logcat? –  Finuka Aug 1 '11 at 15:32
post the logcat to find why it is crashing. –  PravinCG Aug 1 '11 at 15:33
Please show us the log. It might be that there is no last known location. Then you will get null instead. –  TofferJ Aug 1 '11 at 15:33
I'll edit the qn to include the details. –  Brahadeesh Aug 1 '11 at 15:35

2 Answers 2

up vote 3 down vote accepted

If your are testing your app in the Emulator you probably don't have any provider and the currentLocation object is null and that is why getLatitude() and getLongitude() will give a NPE.

Edit: as the @grinnner said, from the DDMS perspective you can simulate the sending of location coordinates. Open the DDMS Perspective in eclipse and in the LocationControls tab set the Longitude and Latitude and then click send. Be sure that the focus in the Devices tab is on the emulator that you are running your app on.

share|improve this answer
He can spoof the location from the DDMS perspective if he uses Eclipse. –  thegrinner Aug 1 '11 at 15:43
Could you tell me how to get a provider if I am using an emulator? –  Brahadeesh Aug 1 '11 at 15:47
@thegrinner I dont know how to do that. Can you explain the steps please? –  Brahadeesh Aug 1 '11 at 15:47
I just have one virtual device. so shouldnt be a problem. I'll try and let you know soon –  Brahadeesh Aug 1 '11 at 15:52
I tried doing that. But I am still getting an error. Any idea when should I send the location parameters? I have the code above in a button onclick listener onClick method. I tried sending before and after I click the button. To no avail. –  Brahadeesh Aug 1 '11 at 15:57

If you don't have a last known location (just do a simple null check for the value returned), then you need to add a location listener to get a location. Something like this:

// Start listening for a new location.
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String bestProvider = lm.getBestProvider(new Criteria(), true);    
mMyLocationListener = new MyLocationListener();
lm.requestLocationUpdates(bestProvider, 0, 0, mMyLocationListener);

private class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {
        // Do what you need to do with the longitude and latitude here.

    public void onProviderDisabled(String provider) {

    public void onProviderEnabled(String provider) {


You should also remember to remove the location listener as soon as you no longer need it:

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mMyLocationListener = null;
share|improve this answer
I dont want location updates. I just want to find the location once. I have this code inside a class that implements a location listener. –  Brahadeesh Aug 1 '11 at 15:46
Even if you only want the location once, I think that this is the only way to get it, if you don't have a last known location. Register a listener and as soon as you get an update, remove that listener and use the location you get. However, if the problem is that the emulator doesn't provide you with one, see Ovidiu's answer. :) –  TofferJ Aug 1 '11 at 15:54
Yeah. Please check my last comment on his answer. Thank you for your answer though. –  Brahadeesh Aug 1 '11 at 15:59

Your Answer


