TradeMe / MapMe

The Android maps adapter
MIT License
841 stars 75 forks source link

IndexOutOfBoundsException with an empty list #5

Closed dellisd closed 6 years ago

dellisd commented 7 years ago

When a MapAdapter is initialized with an empty list of items, it crashes.

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.get(ArrayList.java:437)
    at ca.llamabagel.transpo.features.map.MapAdapter.onCreateAnnotation(MapAdapter.kt:41)
    at nz.co.trademe.mapme.MapMeAdapter.createAnnotation(MapMeAdapter.kt:91)
    at nz.co.trademe.mapme.MapMeAdapter.applyUpdates(MapMeAdapter.kt:126)
    at nz.co.trademe.mapme.MapMeAdapter.consumePendingUpdateOperations$mapme_debug(MapMeAdapter.kt:514)
    at nz.co.trademe.mapme.MapMeAdapter$updateChildViewsRunnable$1.run(MapMeAdapter.kt:502)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:
    at android.app.ActivityThread.main(ActivityThread.java:
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
josh-burton commented 7 years ago

Thanks for the report!

Is this happening after calling notifyDatasetChanged on an empty list? Or simply after attaching the adapter?

josh-burton commented 7 years ago

I've tried to replicate this in the sample app with an empty list, but no luck. Can you confirm that the getItemCount method of the adapter is returning 0 when this happens?

dellisd commented 7 years ago

Maybe I'm going crazy, but I don't seem to be able to reproduce it now (I worked around it). If I come across it again I'll post more info.

josh-burton commented 7 years ago

Ok great, I'll close the issue for now.

dellisd commented 6 years ago

I think I have found the actual cause of the issue. The crash seems to occur when DiffUtil is used on an adapter with an empty list of data.

In the current set up I have, the MapAdapter is initialized in onCreate, attach() and notifyDataSetChanged() are called in getMapAsync. I then obtain data from a local database and then run a DiffUtil on it and dispatch it to the adapter. (Debugging confirmed that it reached the dispatching step).

Once the DiffUtil result was dispatched to the adapter, the app crashes with the same stack trace provided as above.

I think I forgot how to use DiffUtil... my bad.