Android Question [SOLVED] Crash on Statemanager

AHilberink

Active Member
Licensed User
Longtime User
Hi,

I am using Statemanager v1.20.

I got this error message:
B4X:
Error loading state.
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
java.lang.RuntimeException: java.lang.RuntimeException: Cannot parse:  as boolean
    at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1716)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:250)
    at android.app.ActivityThread.main(ActivityThread.java:7755)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Caused by: java.lang.RuntimeException: Cannot parse:  as boolean
    at anywheresoftware.b4a.BA.parseBoolean(BA.java:629)
    at anywheresoftware.b4a.BA.ObjectToBoolean(BA.java:699)
    at ciris.chauffeur.statemanager$ResumableSub_innerRestoreState.resume(statemanager.java:202)
    at ciris.chauffeur.statemanager._innerrestorestate(statemanager.java:85)
    at ciris.chauffeur.statemanager$ResumableSub_innerRestoreState.resume(statemanager.java:397)
    at ciris.chauffeur.statemanager._innerrestorestate(statemanager.java:85)
    at ciris.chauffeur.statemanager$ResumableSub_innerRestoreState.resume(statemanager.java:397)
    at ciris.chauffeur.statemanager._innerrestorestate(statemanager.java:85)
    at ciris.chauffeur.statemanager$ResumableSub_innerRestoreState.resume(statemanager.java:397)
    at ciris.chauffeur.statemanager._innerrestorestate(statemanager.java:85)
    at ciris.chauffeur.statemanager$ResumableSub_innerRestoreState.resume(statemanager.java:359)
    at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1714)
    ... 7 more

After I add a CustomlistView with a panel with Checkbox on it, I got this.
My Panel:
B4X:
Sub CreateItem(h As Int, j As Int, naam As String) As Panel
    Dim pnl As Panel, lblNaam,lblCode As Label, chkGreen,chkRed As CheckBox, TextWidth As Int
    
    pnl.Initialize("")
    pnl.color=Colors.White
    
    IME.Initialize(Me)

    lblNaam.Initialize("") 'View #0
    lblNaam.Text = naam
    lblNaam.TextColor = Colors.DarkGray
    lblNaam.TextSize = 18
    If(Activity.Width>=1200) Then
        lblNaam.TextSize = 22
    End If
    lblNaam.Gravity = Gravity.CENTER_VERTICAL
    TextWidth=clvRoute.sv.Width - 170dip
    pnl.AddView(lblNaam, 0, 0, TextWidth, h)

    lblCode.Initialize("") 'View #1
    lblCode.Text = CodeList.Get(j)
    lblCode.TextColor = Colors.DarkGray
    lblCode.TextSize = 18
    If(Activity.Width>=1200) Then
        lblCode.TextSize = 22
    End If
    lblCode.Gravity = Gravity.CENTER_VERTICAL
    pnl.AddView(lblCode, TextWidth+5dip, 0, 80dip, h)

    chkGreen.Initialize("ChkGreen") 'View #2
    chkGreen.Color=Colors.Green
    pnl.AddView(chkGreen, TextWidth+90dip, 0, 40dip, h)

    chkRed.Initialize("ChkRed") 'View #3
    chkRed.Color=Colors.Red
    pnl.AddView(chkRed, TextWidth+130dip, 0, 40dip, h)

    If(CheckGreen.Get(j) = True) Then chkGreen.Checked=True
    If(CheckRed.Get(j) = True) Then chkRed.Checked=True

    Return pnl
End Sub

Can someone tell me where to start to find the cause?

Kind regards,
André
 

agraham

Expert
Licensed User
Longtime User
B4X:
    If(CheckGreen.Get(j) = True) Then chkGreen.Checked=True
    If(CheckRed.Get(j) = True) Then chkRed.Checked=True
As this code is the only Boolean manipulation in that code and as whatever can't be parsed as Boolean doesn't have a name I would check the those Get(j) calls are returning Booleans and not Nulls.

EDIT: Also the comparison would be better if True is on the left as the type of the left value determines the type of comparison.
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
B4X:
    If(CheckGreen.Get(j) = True) Then chkGreen.Checked=True
    If(CheckRed.Get(j) = True) Then chkRed.Checked=True
As this code is the only Boolean manipulation in that code and as whatever can't be parsed as Boolean doesn't have a name I would check the those Get(j) calls are returning Booleans and not Nulls.

EDIT: Also the comparison would be better if True is on the left as the type of the left value determines the type of comparison.

Thanks. I did check this. I even disabled these lines. Still same problem.

Normal run is no problem. The problem only exist on running StateManager.RestoreState(Activity, "Hoofdscherm", 0)

Kind regards,
André
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
I don't see that detail mentioned, nor it's code, in your post. :( It then seems the problem is not in the code you posted.

First of all: Thank you for helping me!!

The code I use to call StateManager is:
B4X:
Sub imvSynch_Click
    StateManager.RestoreState(Activity, "Hoofdscherm", 0)
    pnlWeging.Top=TopWeging
    StopService(GPSLocatie)                                'TIJDELIJK
    CallSubDelayed(Synchroniseer,"MaakSynchroon")
End Sub

I can also send v1.20 of Statemanager if you wish, but this is the default library.
I don't know what to send more.

The problem only appears after adding the CLV with the panel based on the code in the first post.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The problem appears to be in Sub innerRestoreState which has been called recursively as you can see from the stack trace. As the problem started after you added a Panel it is probably in the last "Else If v Is Panel" code. It might be that StateManager can't cope with a CustomList view. as it was written years ago. Erel says at the top of the StateManager thread

Edit: StateManager was written in 2011. I don't recommend using it for new projects. Use B4XPages + KVS2 instead.

NOTE: The above comment is old and KVS2 is deprecated . Erel really means the internal KeyValueStore library
 
Last edited:
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
The problem appears to be in Sub innerRestoreState which has been called recursively as you can see from the stack trace. As the problem started after you added a Panel it is probably in the last "Else If v Is Panel" code. It might be that StateManager can't cope with a CustomList view. as it was written years ago. Erel says at the top of the StateManager thread

Edit: StateManager was written in 2011. I don't recommend using it for new projects. Use B4XPages + KVS2 instead.

NOTE: The above comment is old and KVS2 is deprecated . Erel really means the internal KeyValueStore library

Thanks again.

These note is new to me. I started the project in 2016 and it is still not B4XPages, because it is large.
I think I need to change my CLV component to a ListView. I know this is a step backwards, but I do not have the possibility to study migration to Pages at this moment.

A quick view at KVS2 looks like I have to build a Statemanager based on this myself or is there already a complete solution to save and restore my activity?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Not KVS2! Use the internal KeyValueStore library v2.31. It is not dependent on B4XPages. Erel always says use B4XPages but if your project exists and doesn't then it's not a problem. Yes you will have to build your own custom state manager as I am not aware of a a generic pre-built version.
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
Not KVS2! Use the internal KeyValueStore library v2.31. It is not dependent on B4XPages. Erel always says use B4XPages but if your project exists and doesn't then it's not a problem. Yes you will have to build your own custom state manager as I am not aware of a a generic pre-built version.

I will check this out. Thanks for pointing my to this direction.
 
Upvote 0
Top