尝试调用虚方法' java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String,int)'在null对象上

时间:2017-03-09 10:08:52

标签: java android

以下是我的代码: http://pastebin.com/kGZY0h88

http://pastebin.com/Y0JNYjEd

每当我尝试使用函数writeAll(ArrayList liste)保存某些东西时,应用程序崩溃。这是日志:

`E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.naabi.listeserie, PID: 3459
                  java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference
                      at com.example.naabi.listeserie.dao.SerialisationSerieDAO.writeAll(SerialisationSerieDAO.java:58)
                      at com.example.naabi.listeserie.descriptionMrRobot.onSaveInstanceState(descriptionMrRobot.java:260)
                      at android.app.Activity.performSaveInstanceState(Activity.java:1414)
                      at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1300)
                      at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:4541)
                      at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4492)
                      at android.app.ActivityThread.-wrap19(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)`

我尝试通过倾斜手机来使用该功能。

1 个答案:

答案 0 :(得分:0)

SerialisationSerieDAO pojo不是活动。因此,您的上下文(contexte)此案例从未初始化。使用mutator设置上下文。或者在构造函数中传递一个。