简体   繁体   中英

Android Directions API ArrayIndexOutOfBoundsException

I am using the Google Directions API. Everything works well until I set the TravelMode to BICYCLING. All other modes work until this attribute is set.

The error I get is: onFailure: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 onFailure: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

Why am I getting this error?

Here is the code:

    private void calculateDirections(Marker marker){
        Log.d(TAG, "calculateDirections: calculating directions.");

        com.google.maps.model.LatLng destination = new com.google.maps.model.LatLng(
                marker.getPosition().latitude,
                marker.getPosition().longitude
        );
        DirectionsApiRequest directions = new DirectionsApiRequest(mGeoApiContext);
        directions  .alternatives(true)
                    .origin(new com.google.maps.model.LatLng(
                        deviceLocation.getLatitude(),
                        deviceLocation.getLongitude()));


        // Set imperial and transport mode
        try {
            if (imperial) {
                directions.units(Unit.IMPERIAL);
                Log.d(TAG, "calculateDirections: imperial set true");
            }
            switch (transportMode){
                case "Drive":
                    directions.mode(TravelMode.DRIVING);
                    Log.d(TAG, "calculateDirections: travelMode: " + transportMode);
                    break;
                case "Walk":
                    directions.mode(TravelMode.WALKING);
                    Log.d(TAG, "calculateDirections: travelMode: " + transportMode);
                    break;
                case "Transit":
                    directions.mode(TravelMode.TRANSIT);
                    Log.d(TAG, "calculateDirections: travelMode: " + transportMode);
                    break;
                case "Cycle":
                    directions.mode(TravelMode.BICYCLING);
                    Log.d(TAG, "calculateDirections: travelMode: " + transportMode);
                    break;
            }
        } catch (Exception e){
            Log.e(TAG, "calculateDirections: imperial error: " + e.getMessage());
        }

        Log.d(TAG, "calculateDirections: destination: " + destination.toString());
        directions.destination(destination).setCallback(new PendingResult.Callback<DirectionsResult>() {
            @Override
            public void onResult(DirectionsResult result) {
                Log.d(TAG, "calculateDirections: onResult: routes: " + result.routes[0].toString());
                Log.d(TAG, "calculateDirections: onResult: duration: " + result.routes[0].legs[0].duration);
                Log.d(TAG, "calculateDirections: onResult: distance: " + result.routes[0].legs[0].distance);
                Log.d(TAG, "calculateDirections: onResult: geocodedWayPoints: " + result.geocodedWaypoints[0].toString());

                addPolyLinesToMap(result);
            }

            @Override
            public void onFailure(Throwable e) {
                Log.e(TAG, "calculateDirections: onFailure: " + e ); 

            }
        });
    }

It's absolutely caused by there is no route for that travel mode (according to Google Direction Api). You have to check if result.routes is null or not before using it.

public void onResult(DirectionsResult result) {
         if (result.routes!=null&&result.routes.length>0){
             //draw polylines
         } else{
             //no route found
         }
}

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