【问题标题】:IllegalArgumentException: invalid provider: null (Only on first launch)IllegalArgumentException:无效的提供者:null(仅在首次启动时)
【发布时间】:2018-01-08 20:06:56
【问题描述】:

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

如果该应用程序以前从未启动过,它会引导您完成一个简短的教程,您可以在 5 个选项卡上滑动,直到看到“知道了!”按钮。如果您之前启动过它,它会直接带您进入地图。它在第一次启动时崩溃,但在重新启动后,它会获取位置并且指南针按钮工作正常。

这里是日志:

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());
         }
       }
    }
});
}

【问题讨论】:

    标签: android google-maps illegalargumentexception


    【解决方案1】:

    我想通了。我在崩溃的行上放置了断点,并意识到我的提供程序始终为空。我赶时间,不记得我刚刚有

    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 成为我的橡皮鸭调试器。我只需要通过它来说服自己。

    【讨论】:

      猜你喜欢
      • 2016-01-26
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多