6

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.

Hardik Mehta
  • 867
  • 1
  • 12
  • 20

1 Answers1

3

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.

Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
  • @ Flame _Phoenix first of all Thank you so much for your answer.since two months no one is answering this question i have to draw red line where user pause his/her workout (As it is fitness App) so i maintain one flag indicating whether at this lat/lng workout is paused or not. 2) point list is list of points to draw route.between start marker and end marker. 3) if map.ispause() return false and last pause return false means workout is running and green polyline added to map – Hardik Mehta Sep 08 '16 at 09:24
  • It may help to change the question a little bit. For example, you mention lite mode, but nothing in your post suggest that is relevant. Then you refer a specific Lenovo model, but the exception is an ArrayIndexOutOfBoundsException. If I were you, I would focus on the exception, changing the title to "ArrayIndexOutOfBounds on Android polyline draw" or something similar, and I would also try to provide a minimum working example of the problem for other people to test on their emulators or devices. Given these tips, I still believe your best course of action is the "What Would I do? section. – Flame_Phoenix Sep 08 '16 at 09:55
  • I edited my answer to fix a typo. Check the Code Evaluation part. As for the exception, does it always occur? If not, in what situations is it triggered? – Flame_Phoenix Sep 08 '16 at 10:00
  • @ Flame_phoenix devices in which i have tested among them i face this issue only in lenovo k5 surely i will check your recommandations to fix this typo and will inform you – Hardik Mehta Sep 08 '16 at 12:19
  • @ Flame_phoenix i have tried you typo and code evaluation but still no sucess – Hardik Mehta Sep 13 '16 at 04:33
  • Do you know where your code crashes now when you evaluate ? In which part of the cycle ? – Flame_Phoenix Sep 13 '16 at 06:29
  • @ Flame_phoenix it is not giving any line number of our class but one thing i observe is after 2 or 3 map loaded it crashes there is no memory issue but strangely it is crashing i m using this method in recyclerview containing many maps – Hardik Mehta Sep 13 '16 at 07:04
  • And it only crashes in that device? Are all the devices you are testing using the same exact version of Android? – Flame_Phoenix Sep 13 '16 at 07:37
  • @ Flame_phoenix yes except intex aqua and samsung cor2 all have same version of os (6.0) – Hardik Mehta Sep 13 '16 at 08:08
  • Can you create a MCVE (http://stackoverflow.com/help/mcve) project in Android that replicates the issue? I am sure this would draw more attention. If you can do it, let me know, I will put a bounty on the question myself. – Flame_Phoenix Sep 14 '16 at 09:03
  • @ Flame_phoenix sure i will create question – Hardik Mehta Sep 14 '16 at 11:28
  • @ Flame_phoenix i tried my best to explan problem here is the link to que http://stackoverflow.com/questions/39489974/strange-arrayindex-out-of-bound-exception-on-some-devices-while-drawing-polyline – Hardik Mehta Sep 14 '16 at 12:03