Crash app when trying to save a file using Objectoutputstream

Asked

Viewed 32 times

0

Mainactivity is 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 not running it works normally.

1 answer

0


The Objectoutputstream can only write to an Outputstream objects that implement the java.io.Serializable interface.

Thus, the class Class and all its properties, which are not primitive types, have to implement Serializable.

  • Thanks for the ramaral help, that’s right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.