문제

I have a problem with a class, log cat return this error when a run

12-05 23:19:18.299: E/AndroidRuntime(15460): Caused by: java.lang.IllegalArgumentException: provider==null
12-05 23:19:18.299: E/AndroidRuntime(15460):    at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1017)
12-05 23:19:18.299: E/AndroidRuntime(15460):    at com.rbrlnx.lugares.editarLugar.onCreate(editarLugar.java:110)
12-05 23:19:18.299: E/AndroidRuntime(15460):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-05 23:19:18.299: E/AndroidRuntime(15460):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)

And the code is

     /************************************************************************************
     * 
     * Creamos un location manager, le establecemos unos criterios minimos para encontra
     * 
     ***********************************************************************************/
    LocationManager locManager = (LocationManager)getSystemService(LOCATION_SERVICE);
    Criteria criterio = new Criteria();
    criterio.setAccuracy(Criteria.ACCURACY_COARSE);
    String mejorProvider = locManager.getBestProvider(criterio, true);

        //Obtenemos la última posición conocida
        final Location loc = locManager.getLastKnownLocation(mejorProvider);
        latitud = loc.getLatitude();
        longitud= loc.getLongitude();

        //Me centro en el mapa
          yo = new GeoPoint(
         (int)(latitud*1000000),
         (int)(longitud*1000000));

Only set the best provider and get the last know position but it crash, anyone knows why?

도움이 되었습니까?

해결책

It's because this line

   String mejorProvider = locManager.getBestProvider(criterio, true);

results in mejorProvider being null. What caused that null? Hard to tell. Either the provider you asked for does not exist or maybe you haven't enabled permissions in your manifest for requesting the user's location.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top