Android Question Can not convert a list to an byte array using b4xserializator

Hi, everyone, this was a problem I had when I was coding following Erel's video tutorial Objects & Custom Types.
mycode:
Dim list1 As List
    list1.Initialize
    For i=1 To 50
        list1.add(createAphone(100+i,700+2*i,"iphone" & i))  'createAphone will create an object of a custom type
    Next
    
    'writebytes
    Dim ser As B4XSerializator
    Dim b() As Byte=ser.ConvertObjectToBytes(list1)    'my app crashed on this line
    Dim out As OutputStream=File.OpenOutput(File.DirInternal,"1.dat",False)
    out.WriteBytes(b,0,b.Length)
    out.Close

And Here's the log
log:
java.lang.RuntimeException: java.lang.RuntimeException: This method does not support arrays of primitives.

I am stuck in this problem, hope someone can help me solve this. Thanks!
 
Solution
Hi, Erel, the custom type
Based on what you are trying to do in post #3, here is a complete working project code:
B4X:
Sub Class_Globals
    Private Root As B4XView  'ignore    
    Type MyPhone(brand As String, mList As List)
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Dim list1 As List
    list1.Initialize
    For i=1 To 50
        list1.add(CreateMyPhone("iphone" & i, Array As Int(100+i,700+2*i))) 
    Next
    
    'writebytes
    Dim ser As B4XSerializator
    Dim b() As Byte=ser.ConvertObjectToBytes(list1)    
    Dim out As OutputStream=File.OpenOutput(File.DirInternal,"1.dat",False)
    out.WriteBytes(b,0,b.Length)
    out.Close
        
    'Read list...
How is the custom type declared?
Hi, Erel, the custom type is like this:
My custom type:
Sub Process_Globals
    Private xui As XUI
    Type Phone(brand As String, size(2) As Int)
End Sub

Sub createAphone(width As Int,height As Int, name As String)As Phone
    Dim p As Phone
    p.Initialize
    p.brand=name
    p.size=Array As Int (width,height)
    Return p
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I am stuck in this problem,
If you use a custom type like this. It will work for you:
B4X:
Type CreateAphone  (Id As Int, Cost As Int, Model As String)

Public Sub CreateAphone (Id As Int, Cost As Int, Model As String) As CreateAphone
    Dim t1 As CreateAphone
    t1.Initialize
    t1.Id = Id
    t1.Cost = Cost
    t1.Model = Model
    Return t1
End Sub
If you need more clarification, come back or post your code.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Hi, Erel, the custom type
Based on what you are trying to do in post #3, here is a complete working project code:
B4X:
Sub Class_Globals
    Private Root As B4XView  'ignore    
    Type MyPhone(brand As String, mList As List)
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Dim list1 As List
    list1.Initialize
    For i=1 To 50
        list1.add(CreateMyPhone("iphone" & i, Array As Int(100+i,700+2*i))) 
    Next
    
    'writebytes
    Dim ser As B4XSerializator
    Dim b() As Byte=ser.ConvertObjectToBytes(list1)    
    Dim out As OutputStream=File.OpenOutput(File.DirInternal,"1.dat",False)
    out.WriteBytes(b,0,b.Length)
    out.Close
        
    'Read list:
    Dim MyList As List
    MyList.Initialize
    MyList= ser.ConvertBytesToObject(b)    
    For i = 0 To MyList.Size - 1
        Dim myphone As MyPhone
        myphone.Initialize
        myphone = MyList.Get(i)
        Log($"Brand: ${myphone.brand} Size:  ${myphone.mList.Get(0)}  ${myphone.mList.Get(1)} "$)
    Next
End Sub

Public Sub CreateMyPhone (brand As String, mList As List) As MyPhone
    Dim t1 As MyPhone
    t1.Initialize
    t1.brand = brand
    t1.mList = mList
    Return t1
End Sub
 
Upvote 0
Solution
T
Thanks, Mahares, your solution works!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…