-1

I am using the following piece of code in my Android application to find my mobile device's current location :

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Double longitude = location.getLongitude();
Double latitude = location.getLatitude();
Log.i("LATITUDE", latitude.toString());
Log.i("LONGITUTDE", longitude.toString());

Up to now, it was working fine, and it was giving me the right coordinates of my location. But before a while and without making any change at all to my code, it started giving me the following error :

java.lang.RuntimeException: Unable to start activity ComponentInfo{di.uoa.gr.e_commerce/di.uoa.gr.e_commerce.FirstResultsActivity}:

java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769) at android.app.ActivityThread.access$900(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5910) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

I do have my WiFi connection open, as well as my Location. Can anyone figure out what could be going wrong?

Marievi
  • 4,951
  • 1
  • 16
  • 33

3 Answers3

2

getLastKnownLocation
[...] Note that this location could be out-of-date, for example if the device was turned off and moved to another location.

developer.android.com

You should rather use one of the requestSingleUpdate methods or of the requestLocationUpdates methods if you need the location more than once. Here is the documentation with all signatures for the mentioned methods.

It would look something like this:

lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);

You would then receive the location in your class. But you class would need to implement LocationListener. As an alternative you could provide an LocationListener as an parameter.

This would look like this:

lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, new LocationListener() {
    @Override
    public void onLocationChanged(Location location)
    {
        
    }
    
    @Override
    public void onStatusChanged(String s, int i, Bundle bundle)
    {
        
    }
    
    @Override
    public void onProviderEnabled(String s)
    {
        
    }
    
    @Override
    public void onProviderDisabled(String s)
    {
        
    }
    }, null);

You then get your location once in onLocationChanged.

Community
  • 1
  • 1
Qup
  • 141
  • 1
  • 7
  • Thanks for your answer. But up to now, I did not implement `LocationListener` and still it worked fine. – Marievi Aug 26 '16 at 06:12
  • I also ocne had the same problem you had but I do not know an explanaition I am sure about. For an possible explanation see the answer from youzking – Qup Aug 26 '16 at 06:18
0

First, you should check that you have the permission to obtain current location, if you're on Android API 23+, you have to ask for it manually.

Secondly, LocationManager.getLastKnownLocation doesn't necessarily returns a valid Location, as it may return "null" is no location is consistent enough for the provider your chose (GPS_PROVIDER). You should fallback to another provider if there is no location here, and try to request a location if nothing if found.

Okaa-Pi
  • 135
  • 10
0
lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

doesn't necessarily returns a Location, the device doesn't have a saved GPS Location, so the method is returning null;

younes zeboudj
  • 856
  • 12
  • 22