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:

    Code:
    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(ArrayList.java:
    255)
        at java.util.ArrayList.get(ArrayList.java:
    308)
        at anywheresoftware.b4a.objects.collections.List.Get(
    List.java:117)
        at b4a.example3.customlistview._findindexfromoffset(customlistview.java:
    475)
        at b4a.example3.customlistview._getfirstvisibleindex(customlistview.java:
    585)
        at b4a.example3.customlistview._updatevisiblerange(customlistview.java:
    1620)
        at b4a.example3.customlistview._scrollhandler(customlistview.java:
    1444)
        at b4a.example3.customlistview._sv_scrollchanged(customlistview.java:
    1596)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    186)
        at anywheresoftware.b4a.BA$
    1.run(BA.java:325)
        at android.os.Handler.handleCallback(Handler.java:
    733)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    136)
        at android.app.ActivityThread.main(ActivityThread.java:
    5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    601)
        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.

    Code:
    'Clears all items.
    Public Sub Clear
        items.Clear
        sv.ScrollViewInnerPanel.RemoveAllViews
        SetScrollViewContentSize(
    0)
    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
    Code:
    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: https://www.b4x.com/android/forum/t...listview-cross-platform-customlistview.84501/

    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:

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

    Code:
    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

Loading...
  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