ArrayIndexOutOfBounds on a Polyline Line on Android

I use the google map lite mode version in which I draw polylines and markers on the Lenovo K50a40 . I get the following error.

**Non-fatal Exception: java.lang.ArrayIndexOutOfBoundsException: length=42; index=42** at maps.ah.ka(Unknown Source:4000) at maps.ah.ka(Unknown Source) at maps.ah.ka(Unknown Source) at maps.ah.ia(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 - my route drawing logic 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; } } } 

devices in which I tested among them, this only happens in the lenovo k50a40 model. I cannot understand the actual reason for any help being appriciated.

+6
source share
1 answer

Why is this a failure?

If you look at the exception, everything indicates that you are moving the Array (or List) incorrectly.

Check the main message

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

Finding out what is happening is more complicated, mainly because we have no information about the code at all:

  at maps.ah.ka(Unknown Source:4000) at maps.ah.ka(Unknown Source) at maps.ah.ka(Unknown Source) at maps.ah.ia(Unknown Source) 

Why does this work on some other devices?

Different Android devices have different software and hardware bases (depending on the company that produces them), and therefore it can be expected that in some cases different types of behavior will be observed.

Thus, while other devices can handle the fatal exception, the Lenovo K50a40 does not seem to be able to. Or it may even be Lenovo's improperly designed system, there is no way to find out.

What should I do?

You can do little. My solution will help you find a way:

  • Place console logs wherever you can.
  • Remove as much logic as possible and add a little bit until you find something that breaks it.
  • Remove forEach in java and force a regular for (int i = 0; i < maps.size(); i++) loop for (int i = 0; i < maps.size(); i++) I can and keep track of i values.

Code rating

This talks about some things that I don’t quite understand 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 Validator.isNull(lastPause) || !lastPause returns false?

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

This will result in a Null Exception.

Can you specify some context not pointList variable?


I know that this is probably not what you hoped for, but I still hope it helps.

+3
source

All Articles