Android Question customlistview error at inserat

leitor79

Active Member
Licensed User
Longtime User
Hello,

I'm getting a strange exception at customlistview module, InsertAt sub, at this line:

B4X:
p.AddView(Pnl, 0, 0, sv.Width, ItemHeight)

The error is the following:
Error occurred on line: 181 (CustomListView)
java.lang.NullPointerException
at anywheresoftware.b4a.objects.ViewWrapper.getWidth(ViewWrapper.java:140)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at eitorteam.privapp.main.afterFirstLayout(main.java:108)
at eitorteam.privapp.main.access$000(main.java:17)
at eitorteam.privapp.main$WaitForLayout.run(main.java:80)
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:5146)
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:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)

I've defined that error as "strange" because it is only raised when the app executes "normally". If I put a breakpoint before the InsertAt method is called, the execution pauses and then I continue it manually, there is no exception and the customlistview loads fine.

I've checked that in:

B4X:
Public Sub InsertAt(Index As Int, Pnl As Panel, ItemHeight As Int, Value As Object)

Value As Object is not null, and that Pnl is also initialized.

I've tried loading the customlistview from Activity_Create and from Activity_Resume, with the same result.

Thank you very much!
 

leitor79

Active Member
Licensed User
Longtime User
Very complicated. Default implementation implies registration (sms verified), having contacts (other people who use the app) and loads the items shown in the list from a webservice (not public published yet)

Regards,
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi,

Yes, the very complicated issue was building a small example.

Here it is.

Thank you very much!
 

Attachments

  • project2.zip
    223.1 KB · Views: 122
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi,

I've been investigating further this issue; I've notices that the error raises in CustomListView module:

B4X:
Public Sub InsertAtTextItem(Index As Int, Text As String, Value As Object)
    Dim pnl As Panel
   
        pnl.Initialize("")
        Dim lbl As Label
        lbl.Initialize("")
        lbl.Gravity = Bit.Or(Gravity.CENTER_VERTICAL, Gravity.LEFT)
        pnl.AddView(lbl, 5dip, 2dip, sv.Width - 5dip, 20dip)  <<<<<<<<<<<<<<<-------------- HERE
        lbl.Text = Text
        lbl.TextSize = DefaultTextSize
        lbl.TextColor = DefaultTextColor
        If DefaultTextBackground <> Null Then
            pnl.Background = DefaultTextBackground
        Else
            pnl.Color = DefaultTextBackgroundColor
        End If
        Dim minHeight As Int
        minHeight = su.MeasureMultilineTextHeight(lbl, Text)
        lbl.Height = Max(50dip, minHeight)
        InsertAt(Index, pnl, lbl.Height + 2dip, Value)
   
End Sub

I've noticed that if I replace the problematic line for this:

B4X:
pnl.AddView(lbl, 5dip, 2dip, 15dip, 20dip)

the error goes away. But I don't like hardcoding views's sizes. How could it be that sv.Width trhows an error? I've checked and sv.IsInitialized is True before that line. How else could I check?

Thank you very much!
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Update; I've read about having to add the customlistview to the activity causing this error. It is not appears to be the problem. If I put a breakpoint before the problem and stop the execution for just 1 second and then continue it, the list loads fine without exceptions.

I've tried to invalidate things (the AHViewPager and the DSTabLayout where the customlistview is), turning off animations (SetVisibleAnimated(0, True) for the viewpager and the tablayout) but with no luck)

Thank you very much
 
Last edited:
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Update; If, from Activity_Create (from where I'm calling the function that load the customlistview), I call the function using CallSubDelayedPlus with 500ms delay ,instead just using the name of the function, the list loads fine without exceptions.

I don't like this "solution". I'd like to figure out what is happening and fix it. I don't know if the 500ms delay will always work or it is device-dependant (and should I use 1000ms... 2000ms... 5000ms...? instead)

Regards,
 
Upvote 0
Top