Android Question Problem saving to .csv after using B4XSerializator

davelew1s

Active Member
Licensed User
Longtime User
I am using bluetooth to send and receive objects using B4XSerializator, after receiving an array I need to save it to a csv file. Saving the file before serializing it works ok but after I get the following error message:-

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
0 1 2 3 4 5
0 1 2 3 4 5
Error occurred on line: 52 (Main)
java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]
at anywheresoftware.b4a.objects.StringUtils.SaveCSV2(StringUtils.java:107)
at anywheresoftware.b4a.objects.StringUtils.SaveCSV(StringUtils.java:100)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at b4a.example1234.main._sendmessage(main.java:397)
at b4a.example1234.main._activity_create(main.java:389)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at b4a.example1234.main.afterFirstLayout(main.java:104)
at b4a.example1234.main.access$000(main.java:17)
at b4a.example1234.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
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)
** Activity (main) Resume **

Here is a test to show the problem

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These vaPrivate rp As RuntimePermissionsriables can be accessed from all modules.
   
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 StringUtils1 As StringUtils
    Public ser As B4XSerializator
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoDim Stats() As String = Array As String(0,1,2,3,4,5)    'Array to sendadLayout("Layout1")
    Dim Stats() As String = Array As String(0,1,2,3,4,5)    'Array to send
    SendMessage (Stats)
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Public Sub NewMessage (stats As Object)
    Dim msg As Object = ser.ConvertBytesToObject(stats)
    Dim a() As String
    a = msg
    Dim lst As List
    lst.Initialize
    lst.Add(a)
    Log(a(0)&"  "&a(1)&"  "&a(2)&"  "&a(3)&"  "&a(4)&"  "&a(5))
    StringUtils1.SaveCSV(File.DirRootExternal,"test2.csv", ";", lst)    'Fails
End Sub

Public Sub SendMessage (stats As Object) 'msg not used in this test

    Dim a() As String
    a = stats
    Dim lst As List
    lst.Initialize
    lst.Add(a)
    Log(a(0)&"  "&a(1)&"  "&a(2)&"  "&a(3)&"  "&a(4)&"  "&a(5))

    StringUtils1.SaveCSV(File.DirRootExternal,"test1.csv", ";", lst)    'Save before sending OK
   
    NewMessage(ser.ConvertObjectToBytes(stats))
End Sub


Any help will be appreciated Dave.
 
Top