IllegalArgumentException:invalid provider:null(仅在首次启动时)

时间:2018-01-08 20:06:57

标签: android google-maps illegalargumentexception

带有自定义指南针按钮的Google Maps API应用程序:

如果该应用程序之前从未启动过,它会引导您完成一个简短的教程,即5个标签,直到达到" Got It!"按钮。如果您之前已经启动过它,它会直接带您到地图上。它在第一次发射时崩溃,但重新启动后,它会获得位置并且指南针按钮正常工作。

这是logcat:

java.lang.IllegalArgumentException: invalid provider: null
at android.location.LocationManager.checkProvider(LocationManager.java:2098)
at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1199)
at com.***********.wheres_my_ride.MapActivity$1.onClick(MapActivity.java:196)

地图活动:

    //Rotate the map back to 0 bearing if rotated
    compass.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {

           if (mMap.getCameraPosition().bearing < 0 || mMap.getCameraPosition().bearing > 0) {

THIS IS WHERE IT CRASHES                            

        try {
             locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
             location = locationManager.getLastKnownLocation(provider); //<-- THE LINE WHERE IT CRASHES
             double camera_latitude = location.getLatitude();
             double camera_longitude = location.getLongitude();
             LatLng cameraLatLng = new LatLng(camera_latitude, camera_longitude);
             CameraPosition cameraPosition = new CameraPosition.Builder()
                     .target(cameraLatLng)
                     .zoom(mMap.getCameraPosition().zoom)
                     .bearing(0)
                     .build();

    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

         } catch (SecurityException e) {
             Log.d(TAG, "instance initializer: " + e.getMessage());
         }
       }
    }
});
}

1 个答案:

答案 0 :(得分:0)

我明白了。我在它崩溃的行上放置了断点,并意识到我的提供者总是为空。我匆匆忙忙,不记得我刚才

String provider;

在类变量中。所以当然它会无效!我删除了

中的provider参数
location = locationManager.getLastKnownLocation(provider);

它有效。这是工作代码:

//Rotate the map back to 0 bearing if rotated
    compass.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mMap.getCameraPosition().bearing < 0 || mMap.getCameraPosition().bearing > 0) {
                try {
                    //locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); //<-- OLD CODE
                    //location = locationManager.getLastKnownLocation(provider); //<-- THE LINE WHERE IT CRASHED (OLD CODE)
                    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //<-- NEW CODE
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); //<-- NEW CODE
                    double camera_latitude = location.getLatitude();
                    double camera_longitude = location.getLongitude();
                    LatLng cameraLatLng = new LatLng(camera_latitude, camera_longitude);
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(cameraLatLng)
                            .zoom(mMap.getCameraPosition().zoom)
                            .bearing(0)
                            .build();
                    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

                } catch (SecurityException e) {
                    Log.d(TAG, "instance initializer: " + e.getMessage());
                }
            }
        }
    });

所以现在,当我第一次启动应用程序并旋转地图时,我的指南针按钮会将地图旋转回0轴承。 : - )

我要感谢Stack Overflow是我的橡皮鸭调试器。我只需要通过它自言自语。