Android Question Issue after writing / reading list

notedop

Member
Licensed User
Longtime User
Hi,
I have a scrollview with radiobuttons. As soon as one of the radiobuttons is ticked, a global list called ListActiveM is updated with the type Genetics (custom type). I have used StateManager to save the state of the radiobuttons whenever the screen is turned. This is working fine. In the Activity_Pause event, I have mentioned to write the ListActiveM to the internal dir whenever application is not closed. If the application is closed, it deletes this same file
In the Activity create, it checks if this file exists. If the file containing the list exists, it loads it into the global ListActiveM.
The list can be viewed by clicking on the Label, it will show a messagebox.
Now what happends:
First I load the application.
After it has been loaded I tick some boxes.
I click the label with text "Name". This shows the msgbox showing all ActivelIstM data.
Then I turn my phone so the screen also turns.
The activity is reloaded.
The radiobuttons state is reloaded correctly
If I tick the label with text "Name"it shows the msgbox with the correct data.
Then if I tick one of the radiobuttons, it gives me the following error:
It seems that it can not properly access the list anymore.

I have attached the project * Removed project again.

B4X:
** Activity (main) Resume **
 
 
main_rb2_checkedchange (B4A line: 406)
 
 
Glist= ListActiveM.Get(i)
java.lang.ClassCastException: java.lang.String
    at com.rvh.agacalc.main._rb2_checkedchange(main.java:1146)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.objects.CompoundButtonWrapper$RadioButtonWrapper$RadioButtonListener.onCheckedChanged(CompoundButtonWrapper.java:143)
    at android.widget.CompoundButton.setChecked(CompoundButton.java:122)
    at android.widget.CompoundButton.toggle(CompoundButton.java:85)
    at android.widget.RadioButton.toggle(RadioButton.java:69)
    at android.widget.CompoundButton.performClick(CompoundButton.java:97)
    at android.view.View.onTouchEvent(View.java:4179)
    at android.widget.TextView.onTouchEvent(TextView.java:6650)
    at android.view.View.dispatchTouchEvent(View.java:3709)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
    at android.app.Activity.dispatchTouchEvent(Activity.java:2068)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679)
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1708)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4595)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    at dalvik.system.NativeStart.main(Native Method)
 
Last edited:

notedop

Member
Licensed User
Longtime User
okay, I fixed it. Because I was trying to write a list containing custom Type, the types were not correctly loaded. I now used RandomAccessFile to write the list as an object:

B4X:
Sub Activity_Pause (UserClosed As Boolean)
 
'statemanager code. It saves latest status.
If UserClosed Then
    StateManager.ResetState("Main")
    File.Delete(File.DirInternal, "ListActiveM.d")
Else
    Dim raf As RandomAccessFile
    raf.Initialize(File.DirInternal, "ListActiveM.d", False)
    raf.WriteObject(ListActiveM, True, raf.CurrentPosition)
    StateManager.SaveState(Activity, "Main")   
End If
StateManager.SaveSettings
 
End Sub

And read it:

B4X:
If File.Exists(File.DirInternal, "ListActiveM.d") Then
    'If the ListActiveM list was saved, reload it
   
    Dim raf As RandomAccessFile
    raf.Initialize(File.DirInternal, "ListActiveM.d", False)
    ListActiveM.Initialize
    ListActiveM = raf.ReadObject(raf.CurrentPosition)
    'ListActiveM.Initialize2(File.ReadList(File.DirInternal, "ListActiveM"))
End If
 
If StateManager.RestoreState(Activity, "Main", 0) = False Then     
    'set the default value  -  only global required.
    ListActiveM.Initialize
 
End If
 
Upvote 0
Top