1

I am trying to acquire my location for an application I am working on. However I receive an error then force close when it tries to select the best provider. Any help on the issue would be much appreciated...do I need to declare something in onCreate in order for it to work? Here is a snippet of code followed by the error:

public void onStart(){
    super.onStart();
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    best = locationManager.getBestProvider(criteria, true);//Selects best location provider given options between GPS and poor man's
    locationProvider = locationManager.getProvider(best);

        if (locationProvider != null) {
            locationManager.requestLocationUpdates(locationProvider.getName(), 60000, 1,
                this.locationListenerRecenterMap);
        } else {
            Log.e(TAG, "NO LOCATION PROVIDER AVAILABLE");
            Toast.makeText(this, "The GPS location provider is not available at this time.", Toast.LENGTH_SHORT).show();
            finish();
        }

    GeoPoint location = this.getLastKnownPoint();
    this.mapController.animateTo(location);
}
public void onResume(){
    super.onResume();
    locationManager.requestLocationUpdates(best, 15000, 1, (LocationListener) this);
}
public void onPause(){
    locationManager.removeUpdates((LocationListener) this);
}
private GeoPoint getLastKnownPoint(){
    GeoPoint lastKnownPoint = GeoUpdateHelper.SCRANTON;
    Location lastKnownLocation = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(lastKnownLocation != null){
    lastKnownPoint = GeoUpdateHelper.getGeoPoint(lastKnownLocation);
    }else{
        lastKnownPoint = GeoUpdateHelper.SCRANTON;
    }
    return lastKnownPoint;
}

And here is the error:

04-16 19:07:25.077: ERROR/AndroidRuntime(4998): Caused by: java.lang.IllegalArgumentException: name==null
04-16 19:07:25.077: ERROR/AndroidRuntime(4998):     at android.location.LocationManager.getProvider(LocationManager.java:324)
04-16 19:07:25.077: ERROR/AndroidRuntime(4998):     at com.example.mapMain.onStart(mapMain.java:76)

EDIT: This is running on my OG Droid. When I click to open the map part of the application which gets the location it force closes.

MByD
  • 135,866
  • 28
  • 264
  • 277
IZI_Shadow_IZI
  • 1,921
  • 4
  • 30
  • 59

2 Answers2

4

The error indicates that no best provider was found, and the call to getBestProvider() returned null.

Although you didn't really specify any criteria, one would expect this call to at least return "something". The javadocs are a bit vague, and don't mention that this method can return null. (looking at the source code, it actually can return null)

However, it is possible that your app / device doesn't have any providers available. Can you execute the following call to check if it returns any providers (it will return all enabled providers, not taking into account any criteria) ?

locationManager.getProviders(true) 

Assuming this is a limitation of the application, double check if you have the following permissions defined, as they will be required to return some kind of provider.

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  
ddewaele
  • 22,363
  • 10
  • 69
  • 82
  • I do have those permissions identified...also coarse location as well. I'm just not seeing where its going wrong... – IZI_Shadow_IZI Apr 16 '11 at 23:42
  • Is this on an emulator or real device ? And are the providers enabled on the emulator/device ? – ddewaele Apr 16 '11 at 23:46
  • Ugh. I'm having the same issue as the op. One thing I noticed about Google is that there seems to be an assumption that their crap will return null if there's an issue, but I'm not sure if that behavior is ever made clear – Joe Plante Mar 18 '13 at 12:48
0

In my case I was testing on a device that didn't have any location services enabled in system settings (Google location services, Standalone GPS services, VZW location services). Therefore requestLocationUpdates was causing the app to crash. I surrounded that code in a try/catch block, and the app no longer crashed.

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
  • Ah sorry, what do you mean by surrounding it with a catch block? I hope you didn't catch the IllegalArgumentException... Despite that that's bad style you could avoid it by checking if you supply NULL. This would also save you performance hence java handles the simple check much more efficient than jumping into the catch-block – Chris Jun 19 '13 at 20:52
  • I hear what you're saying about code style. But performance efficiency in this situation is negligent. – IgorGanapolsky Jun 20 '13 at 16:32
  • 1
    I'm clearly with you that there are some situations where performance matters the most - but Exception handling is expensive, so - if you can - trying to check the parameteres to avoid the exception is faster than getting the exception thrown. Some more exact discussion has been at http://stackoverflow.com/questions/299068/how-slow-are-java-exceptions For the IAE: Maybe this could help you? http://stackoverflow.com/questions/9990129/illegalargumentexception-thrown-by-requestlocationupdate – Chris Jun 26 '13 at 12:37
  • @Chris Thanks for the info. If you look at Android source code for the Camera app here: http://androidxref.com/4.2.2_r1/xref/packages/apps/Camera/src/com/android/camera/LocationManager.java You will see how they use try/catch statements for requestLocationUpdates – IgorGanapolsky Jun 27 '13 at 16:52
  • Ah okay, I withdraw the performance card, here it seems really to be for robustness to catch the exceptions at setup time of the listeners. The API of requestLocationUpdates says: Throws IllegalArgumentException if criteria is null IllegalArgumentException if listener is null SecurityException if no suitable permission is present So despite checking for the permissions its pretty simple to get around the catch-clause if you check for NULL-values - but if the method throws that Exception, it's save to catch it. – Chris Jun 28 '13 at 08:36