Android Question Problem with TableView

HARRY

Active Member
Licensed User
Longtime User
Hi, I constantly gets the error message:
B4X:
Error occurred on line: 234 (Table)
java.lang.RuntimeException: Object should first be initialized (View).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at anywheresoftware.b4a.objects.ViewWrapper.setWidth(ViewWrapper.java:135)
    at b4a.example.table._setcolumnswidths(table.java:2190)
    at b4a.example.tablefr._activity_create(tablefr.java:409)
    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:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    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 b4a.example.tablefr.afterFirstLayout(tablefr.java:102)
    at b4a.example.tablefr.access$000(tablefr.java:17)
    at b4a.example.tablefr$WaitForLayout.run(tablefr.java:80)
    at android.os.Handler.handleCallback(Handler.java:733)

The situation is as follows:
B4X:
Sub Globals
    Dim myTable As Table
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("TableFr")
    Dim head(5) As String  
    myTable.Initialize(Me, "MyTable", 6, Gravity.CENTER_HORIZONTAL, False)  
    myTable.HeaderColor = Colors.Green
    myTable.HeaderTextColor = Colors.Black
    myTable.RowColor1 = Colors.RGB(255,222,173)
    myTable.RowColor2=Colors.RGB(255,222,173)
    myTable.SelectedCellColor=Colors.Yellow
    myTable.SelectedRowColor=Colors.Yellow
    myTable.StatusLineAutoFill=False
    myTable.StatusLine=""
    myTable.TableColor=Colors.Black
    myTable.TextColor=Colors.Black
    myTable.CellAlignment = Bit.Or(Gravity.RIGHT, Gravity.CENTER_VERTICAL)
    myTable.AddToActivity(Activity, 0, 50, Activity.Width, Activity.Height-50)
  
    If  Main.mcode=24 Then
        head(0)="Uur"
        head(1)="Gas m³"
        head(2)="Elec KWh"
        head(3)="° C."
        myTable.SetColumnsWidths(Array As Int(100,185,185,75))
    else if Main.mcode=25 Then
        head(0)="Dag"
        head(1)="Gas m³"
        head(2)="Elec KWh"
        head(3)="° C."
        myTable.SetColumnsWidths(Array As Int(100,185,185,75))
    else if Main.mcode= 26 Then
        head(0)="Jaar"
        head(1)="Week"
        head(2)="Gas m³"
        head(3)="Elec KWh"
        head(4)="° C."
        myTable.SetColumnsWidths(Array As Int(100,100,135,135,75))
    else if Main.mcode=27 Then
        head(0)="Dag"
        head(1)="Gas m³"
        head(2)="Elec KWh"
        head(3)="° C."
        myTable.SetColumnsWidths(Array As Int(100,185,185,75))
    else if Main.mcode=28 Then
        head(0)="Jaar"
        head(1)="Maand"
        head(2)="Gas m³"
        head(3)="Elec KWh"
        head(4)="° C."
        myTable.SetColumnsWidths(Array As Int(100,100,135,135,75))
    End If
    myTable.SetHeader(head)
    myTable.ClearAll
    myTable.Visible=True
End Sub

The error occurs in table.bas at the line v = Header.GetView(col) :
B4X:
Public Sub SetColumnsWidths(Widths() As Int)
    ' clone (keep) Widths
    Dim col, Row As Int
   
    Dim SavedWidths(Widths.Length) As Int
    Dim ColumnWidths(Widths.Length) As Int
    Dim HeaderWidths(Widths.Length) As Int
    If cAutomaticWidths = False Then
        For col = 0 To Widths.Length - 1
            SavedWidths(col) = Widths(col)
            ColumnWidths(col) = Widths(col)
            HeaderWidths(col) = Widths(col)
            DataWidths(col) = Widths(col)
        Next
    Else
        For col = 0 To Widths.Length - 1
            SavedWidths(col) = Widths(col)
            ColumnWidths(col) = Widths(col)
        Next
    End If
   
    Dim v As View
    Dim w As Int
    For col = 0 To Widths.Length - 1
        v = Header.GetView(col)
        w = Max(2dip, Widths(col) - cLineWidth)
        v.Width = w
        If col > 0 Then
            v.Left = Header.GetView(col - 1).Left + Widths(col - 1)
        End If
    Next

I tried to initialize View, but that is not possible.

The error occurs already before the program really starts.

In earlier programs it worked very well. Strange!

Harry
 

stevel05

Expert
Licensed User
Longtime User
Check that Main.mcode has one of the tested values and that the Header is getting set correctly.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Which version of the TableView are you using?

Try to put
myTable.SetHeader(head)
before each
myTable.SetColumnsWidths(...

Be aware that you define the Table with 6 columns in Initialize and you use only 4 or 5 columns.
 
Upvote 0

HARRY

Active Member
Licensed User
Longtime User
The error remains, but if I ignore it everything runs fine.

I corrcted the table definition to have 5 columns. In case I only use 4 I set the unused column to "" as text and 1 as width.

Harry
 
Upvote 0
Top