Android Question Customlistview addtextitem error

donnyy

Member
Licensed User
Longtime User
i have an Activity using TABHOST with customlistview, and i load my data from a sqlite table, i keep getting an error message when i try to add data to the customlistview. I have log the data from the SQL table and don't see any NULL value, please help. This is the error i got:

ordermenu_activity_create (java line: 410)
java.lang.NullPointerException: Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference
at anywheresoftware.b4a.objects.ViewWrapper.getWidth(ViewWrapper.java:140)
at anywheresoftware.b4a.samples.customlistview.customlistview._insertattextitem(customlistview.java:346)
at anywheresoftware.b4a.samples.customlistview.customlistview._addtextitem(customlistview.java:50)
at com.dispatch.dyc.ordermenu._activity_create(ordermenu.java:410)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:179)
at com.dispatch.dyc.ordermenu.afterFirstLayout(ordermenu.java:102)
at com.dispatch.dyc.ordermenu.access$000(ordermenu.java:17)
at com.dispatch.dyc.ordermenu$WaitForLayout.run(ordermenu.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7237)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

This is the code for the Activity:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim i As Int
    Dim liststring As String
   
    If FirstTime Then
        CurrentTab=1
        TabHeight=42
    End If
    For i=0 To TabCount-1
        TabsEnabled(i)=True
        TabsVisibility(i)=True
    Next
   
    outlist.Initialize(Me,"outlist")
    complist.Initialize(Me,"complist")
    dispatch_itDir = File.Combine(File.DirDefaultExternal,"/dispatch_it")
    SQLdb.Initialize(dispatch_itDir, "dispatchit.db",False)   
    dbString= "SELECT * from orderstbl WHERE status<>3"
    Cursors = SQLdb.ExecQuery(dbString)

    For i = 0 To Cursors.RowCount - 1
        Cursors.Position = i
        liststring = "Order #: " & Cursors.GetString("orderno") & "   Pick up Time: " & Cursors.GetString("pickup_time") & CRLF
        liststring = liststring & "Customer: " & Cursors.GetString("custname") & CRLF
        liststring = liststring & "Address: " & Cursors.GetString("custaddr") & CRLF
        liststring = liststring & "Drop off: " & Cursors.GetString("dropaddr") & CRLF
       
        If (Cursors.GetString("comment") <> "") Then
            liststring = liststring & "Comment: " & Cursors.GetString("comment") & CRLF
        End If   
'        Log(liststring)
        outlist.AddTextItem(liststring,Null)
       
        Log("after add")
    Next
    Cursors.Close
 

donnyy

Member
Licensed User
Longtime User
Sorry, forgot to mention, i did create a layout called orderlist which is a tabhost, also a layout called outlist (clv) and a layout called complist (clv) also, through the designer. This is the complete code for the page, I added the data to a string then use AddTextItem to add the string to the clv, after i added the data to the clv, then initialize the Tabhost and add the clv to the tabhost.

B4X:
#Region  Activity Attributes 
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim CurrentTab As Int
    Dim TabCount As Int : TabCount=2
    Dim TabHeight As Int
    Dim TabsEnabled(TabCount) As Boolean
    Dim TabsVisibility(TabCount) As Boolean
    Dim SQLdb As SQL
    Dim dispatch_itDir As String
   
   
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Thost As TabHost
    Dim goback As Button
    Dim TabManager As TabHostExtras
    Dim dbString As String
    Dim Cursors As Cursor
    Dim outlist As CustomListView
    Dim complist As CustomListView
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim i As Int
    Dim j As Int
    Dim liststring As String

    Activity.LoadLayout("OrderList")
    If FirstTime Then
        CurrentTab=1
        TabHeight=42
    End If
    For i=0 To TabCount-1
        TabsEnabled(i)=True
        TabsVisibility(i)=True
    Next
    outlist.AddTextItem("Aaaa", "a")
    outlist.AddTextItem("Aaaa" & CRLF & "Bbbb", "b")
    outlist.AddTextItem("Aaaa" & CRLF & "Bbbb" & CRLF & "Cccc", "c")
    outlist.AddTextItem("Aaaa" & CRLF & "Bbbb" & CRLF & "Cccc" & CRLF & "Dddd" , "d")
    outlist.AddTextItem("Aaaa" & CRLF & "Bbbb" & CRLF & "Cccc" & CRLF & "Dddd" & CRLF & "Eeee", "e")
   
    complist.AddTextItem("don", "a")
    complist.AddTextItem("don" & CRLF & "Bbbb", "b")
    complist.AddTextItem("don" & CRLF & "Bbbb" & CRLF & "Cccc", "c")
    complist.AddTextItem("don" & CRLF & "Bbbb" & CRLF & "Cccc" & CRLF & "Dddd" , "d")
    complist.AddTextItem("don" & CRLF & "Bbbb" & CRLF & "Cccc" & CRLF & "Dddd" & CRLF & "Eeee", "e")

   
    dispatch_itDir = File.Combine(File.DirDefaultExternal,"/dispatch_it")
    SQLdb.Initialize(dispatch_itDir, "dispatchit.db",False)   
    dbString= "SELECT * from orderstbl WHERE status<>3"
    Cursors = SQLdb.ExecQuery(dbString)
    j=0
   
    For i = 0 To Cursors.RowCount - 1
        Cursors.Position = i
        j=i+1
        liststring = "Order #: " & Cursors.GetString("orderno") & "   Pick up Time: " & Cursors.GetString("pickup_time") & CRLF
        liststring = liststring & "Customer: " & Cursors.GetString("custname") & CRLF
        liststring = liststring & "Address: " & Cursors.GetString("custaddr") & CRLF
        liststring = liststring & "Drop off: " & Cursors.GetString("dropaddr") & CRLF
       
        If (Cursors.GetString("comment") <> "") Then
            liststring = liststring & "Comment: " & Cursors.GetString("comment") & CRLF
        End If   
        Log(liststring)
        Log(j)
        outlist.AddTextItem(liststring,j)
       
        Log("after add")
    Next
    Cursors.Close
         
    dbString= "SELECT orderno,custname,custaddr,dropaddr,comment,pickup_time,drop_time from orderstbl WHERE status=3"
    Cursors = SQLdb.ExecQuery(dbString)
    j=0
   
    For i = 0 To Cursors.RowCount - 1
        Cursors.Position = i
        j=i+1       
        liststring = "Order #: " & Cursors.GetString("orderno") & "   Pick up Time: " & Cursors.GetString("pickup_time") & CRLF
        liststring = liststring & "Customer: " & Cursors.GetString("custname") & CRLF
        liststring = liststring & "Address: " & Cursors.GetString("custaddr") & CRLF
        liststring = liststring & "Drop off: " & Cursors.GetString("dropaddr") & CRLF
        liststring = liststring & "Dropped Time: " & Cursors.Getstring("drop_time") & CRLF
       
        If (Cursors.GetString("comment") <> "") Then
            liststring = liststring & "Comment: " & Cursors.GetString("comment") & CRLF
    End If
       
        complist.AddTextItem(liststring,j)
    Next
    Cursors.Close
   
    Thost.Initialize("Thost")
    Thost.AddTab("Tab#1","outlist")
    Thost.AddTab("Tab#2","complist")
    '    set the TabIndicator TextSize to 20dip
    TabManager.setTabTextSize(Thost, 20)
    TabManager.setTabTextColorStateList(Thost, "tab_widget_text_colors")
   
End Sub

Sub Activity_Resume
    Thost.CurrentTab=CurrentTab
    Dim i As Int
    For i=0 To TabCount-1
        TabManager.setTabEnabled2(Thost, TabsEnabled(i), i)
        TabManager.setTabVisibility2(Thost, TabsVisibility(i), i)
    Next
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

donnyy

Member
Licensed User
Longtime User
Here it is Erel. Sorry i was trying to figure it out, so added some extra lines for testing, forgot to take them out. Thanks for checking this for me.
 

Attachments

  • dispatchcode.zip
    25.9 KB · Views: 367
Upvote 0

donnyy

Member
Licensed User
Longtime User
Thanks Erel. I made a mistake on the clv name. My other problem is, i tried to add data to the clv before i add the clv to the tabhost first, so after i switched the order, everything works now.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…