尝试使用ObjectOutputStream保存文件时,应用程序崩溃
主要活动:
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)
负责处理文件的类。
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.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)
错误正好在保存方法中。 当我不运行该方法时,该应用程序正常工作。
我该如何解决这个问题?