Android Question [SOLVED] xCustomListView - How to add from the beggining different images in each item

hatzisn

Well-Known Member
Licensed User
Longtime User
Hi everyone,

I try to use the xCustomListView and add a different image in each item according to its functionality but I get an error.

Here is the code:
Addition of items to the xCustomListView:
Sub cmdComm_Click
    clvComm.Clear
    clvComm.Add(CreateCLVItem(sPhone1, "P"), "tel:" & sPhone1)
    If sPhone2 <> "" Then clvComm.Add(CreateCLVItem(sPhone2, "P"), "tel:" & sPhone2)
    If sEmail <> "" Then clvComm.Add(CreateCLVItem(sEmail, "E"), "mailto:" & sEmail)
    pnlCLV.Visible = True 
End Sub

Sub CreateCLVItem(sPhone As String, ComType As String) As B4XView
    Dim clvi As Panel
    clvi.Initialize("")
  
    clvi.SetLayoutAnimated(0,0,0,100%x,50dip)
    clvi.LoadLayout("CommCLV")
  
    lblPhone.Text = sPhone
    Dim img As ImageView = cmdCommunicate
    Select Case ComType
        Case "P"
            img.Bitmap = LoadBitmap(File.DirAssets,"phone-icon.png")
        Case "E"
            img.Bitmap = LoadBitmap(File.DirAssets,"mail-icon.png")
    End Select
  
    Return clvi
End Sub

In the highlighted line I get the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.res.ColorStateList.getDefaultColor()' on a null object reference
at anywheresoftware.b4a.objects.B4XViewWrapper.getColor(B4XViewWrapper.java:399)
at b4a.example3.customlistview._insertatimpl(customlistview.java:556)
at b4a.example3.customlistview._insertat(customlistview.java:538)
at b4a.example3.customlistview._add(customlistview.java:71)
at dhqi.trial.comm._cmdcallhotel_click(hotel.java:497)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7341)
at android.widget.TextView.performClick(TextView.java:14162)
at android.view.View.performClickInternal(View.java:7307)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27796)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7156)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

What am I doing wrong? I am not familiar with the usage of xCustomListView...

P.S. The xCustomListView is located in a panel in order to be able to hide it and show it at will (No .visible property in xCustomListView)
 

hatzisn

Well-Known Member
Licensed User
Longtime User
Isn't it part of your layout, "CommCLV"?

It should be; you have to declare it (the ImageView) in the Globals routine (from Designer, create member) and simply set its bitmap from inside your "CreateCLVItem"

I tried that because of the error. cmdCommunicate is a part of CommCLV and declared in Globals. Yet I get this error.
 
Last edited:
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
The activity of CommCLV is a gradient filled activity. I cannot check it right now but could this be the problem?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I tried that because of the error. cmdCommunicate is a part of CommCLV and declared in Globals. Yet I get this error.
Anyway, that code should look:
B4X:
Sub CreateCLVItem(sPhone As String, ComType As String) As B4XView
    Dim clvi As Panel  ' WHY clvi? pnlItem or pnlCVItem
    clvi.Initialize("")
  
    clvi.SetLayoutAnimated(0,0,0,100%x,50dip)
    clvi.LoadLayout("CommCLV") ' layCLVItem could be a better name
  
    lblPhone.Text = sPhone
'''    Dim img As ImageView = cmdCommunicate ' cmdCommunicate is an ImageView; ivCommunicate
    Select Case ComType
        Case "P"
            cmdCommunicate.Bitmap = LoadBitmap(File.DirAssets,"phone-icon.png")
        Case "E"
            cmdCommunicate.Bitmap = LoadBitmap(File.DirAssets,"mail-icon.png")
    End Select
  
    Return clvi
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
The activity of CommCLV is a gradient filled activity. I cannot check it right now but could this be the problem?

Yes, it is the problem.
(xCustomListView source code)
B4X:
'Adds a custom item.
Public Sub Add(Pnl As B4XView, Value As Object)
    InsertAt(items.Size, Pnl, Value)
End Sub

Private Sub InsertAtImpl(Index As Int, Pnl As B4XView, ItemSize As Int, Value As Object, InitialSize As Int)
    'create another panel to handle the click event
    Dim p As B4XView = CreatePanel("Panel")
    p.Color = Pnl.Color   '  <------------
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
It is solved.
@Erel I have set targetSdkVersion to 29
@LucaMs Thank you for the responses. The error was that if either you set gradient fill in the CustomListView in the designer or in the panels you add to it, the big bang occurs...

Also the code was changed to:

Addition of items to the xCustomListView:
Sub cmdComm_Click
    clvComm.Clear
    clvComm.Add(CreateCLVItem(sPhone1, "P"), "tel:" & sPhone1)
    If sPhone2 <> "" Then clvComm.Add(CreateCLVItem(sPhone2, "P"), "tel:" & sPhone2)
    If sEmail <> "" Then clvComm.Add(CreateCLVItem(sEmail, "E"), "mailto:" & sEmail)
    pnlCLV.Visible = True
End Sub

Sub CreateCLVItem(sPhone As String, ComType As String) As B4XView
    Dim clvi As B4XView
    clvi = xu.CreatePanel("")

    clvi.SetLayoutAnimated(0,0,0,clvComm.AsView.Width,50dip)
    clvi.LoadLayout("CommCLV")

    lblPhone.Text = sPhone
    Dim img As ImageView = cmdCommunicate
    Select Case ComType
        Case "P"
            img.Bitmap = LoadBitmap(File.DirAssets,"phone-icon.png")
        Case "E"
            img.Bitmap = LoadBitmap(File.DirAssets,"mail-icon.png")
    End Select

    Return clvi
End Sub
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
path of android.jar in configure paths
This is not relevant. Read this tutorial: android.jar / targetSdkVersion / minSdkVersion

The problem is indeed related to the gradient drawable. Starting from targetSdkVersion 29 it is not possible to try and get the color as currently done. It was done with reflection and the API became locked.

Anyway, it is a mistake to set targetSdkVersion to 29 for now.
 
Upvote 0
Top