Android Question undo manager class

Steve Musitano

New Member
Licensed User
Longtime User
Hi
My first post...
Seemed to have solved all other issues, but hopefully someone can help with htis one regarding undo manager class from Erel.
Background, the app has (at present) 6 buttons, that are basically counters. UndoManager has been working fine, with the buttons declared as buttons. However i have change the buttons to an array so i can manage the code better elsewhere and give me flexibilty to add more buttons easily.
I now get and error when getState is called to read the button settings.
I can see the error says "This method does not support arrays of primitives." is thier a way round this or solution???

Thanks in advance
steve

This is the error, line 2 in add state is where it errors
*** mainpage: B4XPage_Disappear [mainpage]
*** catchtrack: B4XPage_Appear [mainpage]
Error occurred on line: 15 (UndoManager)
java.lang.RuntimeException: java.lang.RuntimeException: This method does not support arrays of primitives.
at anywheresoftware.b4a.randomaccessfile.B4XSerializator.writeType(B4XSerializator.java:285)
at anywheresoftware.b4a.randomaccessfile.B4XSerializator.writeObject(B4XSerializator.java:241)
at anywheresoftware.b4a.randomaccessfile.B4XSerializator.WriteObject(B4XSerializator.java:121)
at anywheresoftware.b4a.randomaccessfile.B4XSerializator.ConvertObjectToBytes(B4XSerializator.java:79)

getState with the button array:
Sub GetState As UndoData
    Dim ud As UndoData
    ud.Initialize
    For i = 1 To 6
        ud.Net_txt(i) = btnNet(i).Text
        ud.Net_sz(i) = btnNet(i).TextSize
        ud.Net_clr(i) = btnNet(i).TextColor
        ud.Net_count(i) = Ncount(i)
    Next
    ud.TotalCount = TotalCount
    ud.TotalC_sz = btnTotalC.TextSize
    ud.TotalC_clr = btnTotalC.TextColor
    Return ud
End Sub

addstate in undomanager:
Public Sub AddState (state As Object)
    Dim b() As Byte = ser.ConvertObjectToBytes(state)
    If DifferentThanPrevState(b) Then
        If index < stack.Size - 1 Then
            'this happens when a new state is added after one or more undo actions.
            For i = stack.Size - 1 To index + 1 Step - 1
                stack.RemoveAt(i)
            Next
        End If
        stack.Add(b)
        If stack.Size >= MAX_STACK_SIZE Then
            stack.RemoveAt(1) 'keep the initial state
        End If
        index = stack.Size - 1
    End If
    Log($"Stack size: ${stack.Size}"$)
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Without knowing what you changed in UndoData and how you are filling it with buttons it is hard to give any congrete advice.

Upload a small project showing the issue or post all relevant code.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Instead of creating an array try using a List with your buttons.
 
Upvote 0

Steve Musitano

New Member
Licensed User
Longtime User
Upload a small project showing the issue or post all relevant code.
I have attached a cut down of the project which shows it working, and if you swap out the commented bits it will fail with the error.
Instead of creating an array try using a List with your buttons.
Could you elaborate, or give an example of what you mean
 

Attachments

  • examp.zip
    15.5 KB · Views: 10
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Could you elaborate, or give an example of what you mean
Tomorrow when possible I will try to check your example.
Right now I have no possibilities.
But as a concept, where you can use an array you can quite for sure use a List.
And a List should be accepted as item to be Serialized, while a pure array can't.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
and if you swap out the commented bits it will fail with the error
B4X:
'        ud.Net_txt(i) = btnNet(i).Text        '
ud.Net_txt(i)
You are accessing an array here but you only defined a STRING for Net_txt
B4X:
    Type UndoData (Net1_txt As String, Net1_sz As String, Net1_clr As String, _    '
    Net2_txt As String, Net2_sz As String, Net2_clr As String)                    'comment these out
I don´t see any array in the definition...

This can not work.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Steve Musitano

New Member
Licensed User
Longtime User
So with some help from Claude i now have the example working, ended up using a Map.
I have uploaded the working example, as it may help someone else in the future
 

Attachments

  • exampsolved.zip
    16.3 KB · Views: 6
Upvote 0
Top