2

My app crashes when I try to get my location on a samsung galaxy s3. It works perfect on 3 other phones that I have tested. I just have remote testers on the samsung galaxy s3 so I can't get the error message. Here's my code:

    Location finalLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    locationManager.removeUpdates(locationListenerGps);
    locationManager.removeUpdates(locationListenerNetwork);

    Location net_loc=null, gps_loc=null;
    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
        gps_loc=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
        net_loc=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    //if there are both values use the latest one
    if(gps_loc!=null && net_loc!=null){
        if(gps_loc.getTime()>net_loc.getTime())
            finalLocation = gps_loc;
        else
            finalLocation = net_loc;
    }

    if(gps_loc!=null){
        finalLocation = gps_loc;

    }
    if(net_loc!=null){
        finalLocation = net_loc;

    }
Marcelo
  • 9,387
  • 3
  • 35
  • 40

2 Answers2

1

I think this is a "feature" with the 4.0.4 release, check(getLastKnownLocation does not work on Samsung Galaxy S3 4.0.4).

I had the same problem and it went away when I upgraded to 4.1.

you probably must catch the case where both are null, I found that PASSIVE_PROVIDER works.

   if ( gps_loc == null && net_loc == null ) { // only S3 gets this....
      finalLocation=  locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
   }
   if ( finalLocation == null ) panic....
Community
  • 1
  • 1
Nys
  • 556
  • 4
  • 10
0

It is hard to tell the exact issue without a logcat, but getLastKnownLocation can return null in some cases (not likely but possible). If that is the case here it is easy to see that would cause a crash. Maybe try adding in some code to check that getLastKnownLocation returned a value and if not either use a dummy value or 'pause' your actions and grab the location yourself.

http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String)

MikeIsrael
  • 2,871
  • 2
  • 22
  • 34