简体   繁体   中英

IllegalArgumentException: invalid provider: null (Only on first launch)

Google Maps API application with a custom compass button:

If the app has never been launched before, it takes you through a short tutorial, which is 5 tabs you swipe through until reaching a "Got It!" button. If you've launched it before, it takes you straight to the map. It crashes on the first launch, but after restarting, it then gets the location and the compass button works fine.

Here is the 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)

Map Activity:

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

I figured it out. I placed breakpoints on the line where it crashed, and realized that my provider was always null. I got in a hurry and didn't remember that I just had

String provider;

in the class variables. So of course it's gonna be null! I removed the provider parameter in

location = locationManager.getLastKnownLocation(provider);

and it worked. Here is the working code:

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

So now, when I launch the app for the first time, and I rotate my map, my compass button rotates the map back to 0 bearing. :-)

I would like to thank Stack Overflow for being my rubber duck debugger. I just needed to talk myself through it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM