Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayIndexOutOfBounds on Android polyline draw

I am using google map lite mode version in which i am drawing poly lines and markers on Lenovo K50a40 I m receiving following error

  **Non-fatal Exception: java.lang.ArrayIndexOutOfBoundsException: length=42; index=42**
   at maps.ah.k.a(Unknown Source:4000)
   at maps.ah.k.a(Unknown Source)
   at maps.ah.k.a(Unknown Source)
   at maps.ah.i.a(Unknown Source)
   at maps.ah.d.onDraw(Unknown Source)
   at android.view.View.draw(View.java:16457)
   at android.view.View.buildDrawingCacheImpl(View.java:15722)
   at android.view.View.buildDrawingCache(View.java:15576)
   at android.view.View.draw(View.java:16204)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3746)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3536)
   at android.view.View.updateDisplayListIfDirty(View.java:15402)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3730)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3710)
   at android.view.View.updateDisplayListIfDirty(View.java:15361)
   at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:286)
   at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:292)
   at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:327)
   at android.view.ViewRootImpl.draw(ViewRootImpl.java:3024)
   at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2828)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2440)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1325)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6741)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:912)
   at android.view.Choreographer.doCallbacks(Choreographer.java:714)
   at android.view.Choreographer.doFrame(Choreographer.java:649)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:898)
   at android.os.Handler.handleCallback(Handler.java:815)
   at android.os.Handler.dispatchMessage(Handler.java:104)
   at android.os.Looper.loop(Looper.java:207)
   at android.app.ActivityThread.main(ActivityThread.java:5769)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

and below is my logic for drawing route on google map

 public static void drawRouteIntoMap(List<? extends MapHelper> position, final GoogleMap googleMap , final boolean removeMarker) {
        /*List<MapHelper> position = new ArrayList<MapHelper>();
        for (int i = lastPosition; i < maps.size(); i++) {
            position.add(maps.get(i));
        }*/
        if (position.size() > 0 && Validator.isNotNull(googleMap)) {
           // googleMap.clear();
            final List<? extends MapHelper> tempList=new ArrayList<>(position);
            if(tempList.size()!=pointList.size()) {
                tempList.removeAll(pointList);
            }

            List<PolylineOptions> polylineOptionses = new ArrayList<PolylineOptions>();
            PolylineOptions option = null;
            Boolean lastPause = null;
            for (MapHelper map : tempList) {
                if (map.isPause()) {
                    if (Validator.isNull(lastPause) || !lastPause) {
                        option = new PolylineOptions().width(5).color(Color.rgb(255, 0, 155)).geodesic(true);
                        polylineOptionses.add(option);
                    }
                    option.add(new LatLng(map.getLatitude(), map.getLongitude()));
                } else {
                    if (Validator.isNull(lastPause) || lastPause) {
                        option = new PolylineOptions().width(5).color(Color.rgb(0, 179, 253)).geodesic(true);
                        polylineOptionses.add(option);
                    }
                    option.add(new LatLng(map.getLatitude(), map.getLongitude()));
                }
                lastPause = map.isPause();
            }
            for (PolylineOptions options : polylineOptionses) {
                googleMap.addPolyline(options);
            }
            if(Validator.isNotNull(option)) {
                List<LatLng> points = option.getPoints();
                final LatLngBounds.Builder mapBounds = new LatLngBounds.Builder();
                for (LatLng latLng : points) {
                    mapBounds.include(latLng);
                }

                googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                    @Override
                    public void onMapLoaded() {
                        if(removeMarker) {
                            if (Validator.isNotNull(endMarker)) {
                                startMarker.remove();
                                endMarker.remove();
                            }
                        }
                        LatLng startPoint = new LatLng(tempList.get(0).getLatitude(), tempList.get(0).getLongitude());
                        Marker marker1=googleMap.addMarker(new MarkerOptions().position(startPoint).title("start").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
                        LatLng endPoint = new LatLng(tempList.get(tempList.size() - 1).getLatitude(), tempList.get(tempList.size() - 1).getLongitude());
                        Marker marker2=googleMap.addMarker(new MarkerOptions().position(endPoint).title("finish").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                        googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
                        googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
                        googleMap.moveCamera(CameraUpdateFactory.zoomOut());
                        startMarker=marker1;
                        endMarker=marker2;

                    }
                });

                pointList = position;

            }

        }
    }

the devices in which i performed testing among them it only occurs only in lenovo k50a40 model i am not able to figure it out actual cause any help is appriciated.

like image 829
Hardik Mehta Avatar asked Jul 08 '16 07:07

Hardik Mehta


1 Answers

Why does it crash?

By looking at the exception, everything indicates that you are traversing an Array (or List) incorrectly.

Check the main message

java.lang.ArrayIndexOutOfBoundsException: length=42; index=42

Figuring out what goes after, is more tricky, mainly because we have no information about the code whatsoever:

   at maps.ah.k.a(Unknown Source:4000)
   at maps.ah.k.a(Unknown Source)
   at maps.ah.k.a(Unknown Source)
   at maps.ah.i.a(Unknown Source)

Why does it work on some other devices?

Different Android devices have different software bases and hardware bases (depending on the company that manufactures them) and so one can expect to see different behaviors at some points.

Thus, even though other devices can deal with the non-fatal exception, Lenovo K50a40 seems not be able to. Or it may even be a Lenovo system incorrectly designed, there is no way to know.

What would I do?

There is little you can do. My solution to help you find the path is to:

  1. Place console logs everywhere you can.
  2. Remove as much logic as possible and add little by little until you find something that breaks it.
  3. Remove the forEach in java and force a regular for (int i = 0; i < maps.size(); i++) loop everywhere I can, and monitor for the values of i.

Code evaluation

This said, there are some things I don't quite get from your code.

if (map.isPause()) {
    if (Validator.isNull(lastPause) || !lastPause) {
        option = new PolylineOptions().width(5).color(Color.rgb(255, 0, 155)).geodesic(true);
        polylineOptionses.add(option);
    }
    option.add(new LatLng(map.getLatitude(), map.getLongitude()));
} else {
    if (Validator.isNull(lastPause) || lastPause) {
        option = new PolylineOptions().width(5).color(Color.rgb(0, 179, 253)).geodesic(true);
        polylineOptionses.add(option);
    }
    option.add(new LatLng(map.getLatitude(), map.getLongitude()));
}

What happens if map.isPause() returns true and Validator.isNull(lastPause) || !lastPause returns false?

If I understand correctly, you will be running option.add(new LatLng(map.getLatitude(), map.getLongitude())); on a null object.

This will cause a Null Exception.

Can you give some context not pointList variable?


I know this is probably not what you were hoping for, but I still hope it will help.

like image 144
Flame_Phoenix Avatar answered Oct 26 '22 03:10

Flame_Phoenix