App crashing when trying to save a file using ObjectOutputStream

0

MainActivity looks like this:

    turma.alunos.add(Aluno("Weslley", "123"))
    turma.alunos.add(Aluno("Thiago", "1235"))
    turma.alunos.add(Aluno("Thayane", "1234"))
    turma.alunos.add(Aluno("Kelvin", "1253"))
    turma.alunos.add(Aluno("Carlos", "12253"))

    var arq = ArquivoUtils(turma, this.applicationContext)

    recycler_view.layoutManager = LinearLayoutManager(this)
    recycler_view.adapter = AlunoAdapter(this,turma.alunos)

Class responsible for handling the file

class ArquivoUtils internal constructor( var turminha: Turma, var context : Context) {

internal val fos = this.context.openFileOutput("turma.dat", Context.MODE_PRIVATE)
internal val oos = ObjectOutputStream(fos)

init {
    //this.criarArquivo()
    this.salvarArquivo()
    this.closeFile()
}

@Throws(IOException::class)
private fun salvarArquivo() {
    oos.writeObject(turminha)
}

@Throws(IOException::class)
fun closeFile() {
    oos.close()
}

Logcat:

  --------- beginning of crash
09-22 01:09:07.626 2601-2601/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: com.example.thial.estudandokotlin, PID: 2601
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thial.estudandokotlin/com.example.thial.estudandokotlin.MainActivity}: java.io.NotSerializableException: com.example.thial.estudandokotlin.Aluno
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:154)
                                                 at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

The error is exactly in the save method, when I do not run it works normally.

    
asked by anonymous 22.09.2017 / 03:12

1 answer

0

The ObjectOutputStream can only write to an OutputStream object that implements the java.io interface. Serializable.

So, the Class class and all its properties, other than primitive types, have to implement Serializable.

    
22.09.2017 / 11:39