Android Question Clearing a xCustomListView with lazy loading make the app crash

Discussion in 'Android Questions' started by Martin Larsen, Feb 28, 2018.

  1. Martin Larsen

    Martin Larsen Active Member Licensed User

    VisibleRangeChanged is a godsend for my project as I was facing out of memory issues with my xCLV.

    However, there is a problem as calling CLV.clear makes the app crash with the following exception:

    customlistview_findindexfromoffset (B4A line: 370)
    Dim CurrentItem As CLVItem = items.Get(Position)
    java.lang.IndexOutOfBoundsException: Invalid index 
    0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(
        at java.util.ArrayList.get(
        at anywheresoftware.b4a.objects.collections.List.Get(
        at b4a.example3.customlistview._findindexfromoffset(
        at b4a.example3.customlistview._getfirstvisibleindex(
        at b4a.example3.customlistview._updatevisiblerange(
        at b4a.example3.customlistview._scrollhandler(
        at b4a.example3.customlistview._sv_scrollchanged(
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(
        at anywheresoftware.b4a.BA.raiseEvent2(
        at anywheresoftware.b4a.BA$
        at android.os.Handler.handleCallback(
        at android.os.Handler.dispatchMessage(
        at android.os.Looper.loop(
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(
        at dalvik.system.NativeStart.main(Native Method)
    It has nothing to do with VisibleRangeChanged as this never called after clearing the CLV. The app crashes as soon as CLV.Clear is called.

    Note: The problem only occurs if the CLV has been scrolled before called Clear.

    I have attached an example based on the original demo code.

    Attached Files:

  2. DonManfred

    DonManfred Expert Licensed User

    Again here.

    'Clears all items.
    Public Sub Clear
    End Sub
    This is the code from xcustomlistview class. It does NOT access item 0 from any list like in your exception. The error happens somewhere else

    Based on the error it happens in the line
    Dim CurrentItem As CLVItem = items.Get(Position)
    ETA: The code does not exists in clv Class...
  3. DonManfred

    DonManfred Expert Licensed User

    B4A line: 370

    Check the generated java source to find out which line exactly raises the error....
  4. Martin Larsen

    Martin Larsen Active Member Licensed User

    Thanks for your assistance, I really appreciate it!

    I am fully aware that the error takes place at line 370 of CustomListView.bas (I always click on the exception to go to the source code).

    But what I mean is that this file is the library from Erel's orginial post introducing xCustomListView:

    I use the original library and the original example, only modified as per Erel's tutorial as adviced here.

    Sorry, I don't know what ETA means except estimated time of arrival which I guess is not the meaning here :)
    Multiverse app likes this.
  5. Multiverse app

    Multiverse app Active Member Licensed User

    I am having the same crash in clv.clear
  6. Martin Larsen

    Martin Larsen Active Member Licensed User

    I have found that sometimes debugging is easier with the legacy debugger as it shows you a call stack at the time of the exception instead of just stopping.

    The problem only occurs if the topmost index is not zero. Only then the following is triggered from the sv in line 468 of CustomListView.bas:

    Private Sub sv_ScrollChanged(Position As Int)
    End Sub
    which in turns calls ScrollHandler and then UpdateVisibleRange. Modifying UpdateVisibleRange in line 254 fixes the problem:

    Private Sub UpdateVisibleRange
    If MonitorVisibleRange = False Or items.Size = 0 Then Return ' if there are no items, do nothing!
    Erel, please check if this is a correct fix of the problem in the library or it should be handled somewhere else.
    Multiverse app and DonManfred like this.
  7. Erel

    Erel Administrator Staff Member Licensed User

    Your fix should be good.

    I will add it to ScrollHandler directly as none of the related events should be raised when the list is empty.
  8. Erel

    Erel Administrator Staff Member Licensed User

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice