java.lang.IllegalArgumentException:provider == null

时间:2011-12-05 22:40:15

标签: android gps locationmanager

我遇到类的问题,log cat在运行时返回此错误

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)

代码是

     /************************************************************************************
     * 
     * 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));

只设置最好的提供商并获得最后知道的位置但它崩溃,任何人都知道为什么?

1 个答案:

答案 0 :(得分:5)

这是因为这一行

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

导致mejorProvider为空。是什么导致了这个?很难说。您要求的提供商不存在,或者您未在清单中启用权限以请求用户的位置。