Kotlin-应用程序在ObjectInputStream行启动时崩溃

时间:2019-02-11 08:36:58

标签: kotlin

我的应用程序在启动时在ObjectInputStream行崩溃,我不确定为什么会这样,它在其上方调用openInputFile命令来获取所需的文件,但在启动时崩溃了整个问题。

我尝试从该行中删除活动标签,但到目前为止还没有任何作用

        private fun loadList() {
            try {
        val fileInputStream = activity?.openFileInput("starWarsList.dat")
        val objectInputStream = ObjectInputStream(fileInputStream)

        @Suppress("UNCHECKED_CAST")
        val list = objectInputStream.readObject() as? MutableList<StarWarsItem>
        if (list != null) {
            adapter.list = list
        }
        objectInputStream.close()
        fileInputStream?.close()
    } catch (e: java.io.FileNotFoundException) {
        Toast.makeText(activity, "no existing list found", Toast.LENGTH_LONG).show()
    }
}

最终结果是应用程序正确加载,因此我可以完成项目的其余部分,但是如上所述,应用程序在启动时在此行崩溃。

非常感谢任何投入。

编辑: 启动时收到错误

2019-02-11 08:14:51.432 19116-19116/com.example.assignment E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.assignment, PID: 19116
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:503)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
     Caused by: java.io.EOFException
        at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2344)
        at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2813)
        at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
        at java.io.ObjectInputStream.<init>(ObjectInputStream.java:301)
        at com.example.assignment.StarWarsList.loadList(StarWarsList.kt:52)
        at com.example.assignment.StarWarsList.onActivityCreated(StarWarsList.kt:46)
        at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2460)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1483)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3269)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
        at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
        at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
        at android.app.Activity.performStart(Activity.java:7157)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2937)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
2019-02-11 08:14:51.513 19116-19116/com.example.assignment I/Process: Sending signal. PID: 19116 SIG: 9

1 个答案:

答案 0 :(得分:0)

您似乎有一个空文件,没有要序列化的内容。我建议在读取文件之前转储.dat文件的二进制内容或其长度。

如果无效,请将其替换为有效的默认值。