0

Why is the value of my location variable null? I'm setting up my LocationManager correctly.

Here is my source code:

    //Get the current location
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    myCriteria = new Criteria();
    String provider = locationManager.getBestProvider(myCriteria, true);
    Location location = locationManager.getLastKnownLocation(provider);
    boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
    //Location location;

    if (isGpsEnabled) { 
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    }
    else { 
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }

    Log.i(TAG, "Provider is: "+provider);
    Log.i(TAG, "Location is: "+location);

Here is the logcat output:

06-17 10:40:52.637: D/dalvikvm(15106): GC_FOR_ALLOC freed 534K, 7% free 10586K/11360K, paused 16ms, total 16ms
06-17 10:40:52.677: I/OGT.RideTracking(15106): Provider is: gps
06-17 10:40:52.677: I/OGT.RideTracking(15106): Location is: null
06-17 10:40:52.677: D/AndroidRuntime(15106): Shutting down VM

What is wrong with my code?

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
user268397
  • 1,917
  • 7
  • 38
  • 56
  • possible duplicate of [NullPointerException - Location - getLatitude() method](http://stackoverflow.com/questions/17150066/nullpointerexception-location-getlatitude-method) – Blackbelt Jun 17 '13 at 15:54
  • 1
    there is no warranty that `getLastKnownLocation`gives you a location. If it is `null`you should call `locationManager.requestLocationUpdates()` and wait the provider gets the location. – rciovati Jun 17 '13 at 15:56
  • 2
    Do you have ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions set? – neo Jun 17 '13 at 15:56
  • Yes I do have those permissions in the Manifest file – user268397 Jun 17 '13 at 16:00
  • you are probably getting null because there is currently no lastKnownLocation. Open up google maps or something that uses GPS and it will probably work – tyczj Jun 17 '13 at 16:07
  • I've been running it on a tablet and the error I get above is the one I get. – user268397 Jun 17 '13 at 16:09
  • Look at "Location Strategies" in the Android Development site: http://developer.android.com/guide/topics/location/strategies.html – DigCamara Jun 17 '13 at 16:10
  • Hello user268397 `[android-maps-extensions]` tag is meant for [Android Maps Extensions](http://code.google.com/p/android-maps-extensions/) library. I have removed unnecessary tag from your questions. – MaciejGórski Jun 17 '13 at 17:04

2 Answers2

1

Try this Code

These variable are class variables

LocationManager locManager;
LocationListener locListener;

Add this code on create method

locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);

In the same file add this code

public class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location loc) {
        Log.d("tag", "Finding Latitude");
        double lat = loc.getLatitude();
        Log.d("tag", "Lat: " + String.valueOf(lat));
        Log.d("tag", "Finding Longitude");
        double lon = loc.getLongitude();
        Log.d("tag", "Lon: " + String.valueOf(lon));
        Log.i(Tag, loc.toString());
        String Text = "Lat: " + lat + "\nLon: " + lon;

        // Display location
        locManager.removeUpdates(locListener);
    }

    public void onProviderDisabled(String provider) {

    }

    public void onProviderEnabled(String provider) {

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {

    }
}

AndroidManifest.xml Requires

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
MDMalik
  • 3,951
  • 2
  • 25
  • 39
0
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

Check if gps is enabled.

neo
  • 1,952
  • 2
  • 19
  • 39
  • But you call Location location = locationManager.getLastKnownLocation(provider); without checking if gps is enabled. – neo Jun 17 '13 at 16:09