为什么我的对象不保存到持久存储?

Why won't my object save to persistent storage?

我有一个复杂的对象,我们可以调用它 MyComplexObject。我想将它保存到持久存储中,以便在应用程序被销毁并重新创建后我可以检索它。我正在使用这些方法在活动之间传递我的对象(在一个 activity 中保存并在下一个中加载)。我还想在应用程序被销毁时保存对象(可能在 onStop() 覆盖中) 我可以在活动之间成功加载和保存我的对象,但由于某种原因,如果我销毁 phone 上的应用程序并重新启动它,总是会创建一个新的 MyComplexObject 并且它无法加载在它被销毁之前保存的对象!

我的对象包含多个对象,这些对象包含多个对象 - 所有对象都实现 Serializable

关于如何能够 save/load 我的对象以允许我在应用程序 destroyed/recreated 后保留玩家信息的任何想法?

即使 onDestroy() and/or onStop() 我也收到了对象确实已保存的通知!所以我不确定为什么它在重新创建应用程序时无法加载它。

 public static MyComplexObject Load(Context context){
        try{
            FileInputStream fis = context.getApplicationContext().openFileInput("player1.data");
            ObjectInputStream is = new ObjectInputStream(fis);
            MyComplexObject Game = (MyComplexObject) is.readObject();
            is.close();
            fis.close();
            return Game;
        }catch (Exception e){
            Log.e("Load", "creating new game - \n" + e.toString());
            MyComplexObject NewGame = MyComplexObject();
            return NewGame;
        }
    }


    public static void Save(Context context,MyComplexObject Game){
        try{
            FileOutputStream fos = context.getApplicationContext().openFileOutput("player1.data", Context.MODE_PRIVATE);
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(Game);
            os.close();
            fos.close();
        }catch (Exception e){
            Log.e("Save", "Failed to save - \n" + e.toString());
        }
    }

这是日志:

07-01 14:24:11.824    3304-3304/? E/#Load﹕ creating new world -
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

这是我的 onCreate() 片段:

MyComplexObject Game;

      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Game =  LoadGame.Load(this);  //LoadGame is the class with Load(),Save()
    }

尝试:

public static MyComplexObject Load(Context context){
    try{
        FileInputStream fis = context.openFileInput("player1.data");
        ObjectInputStream is = new ObjectInputStream(fis);
        MyComplexObject Game = (MyComplexObject) is.readObject();
        is.close();
        fis.close();
        return Game;
    }catch (Exception e){
        Log.e("Load", "creating new game - \n" + e.toString());
        MyComplexObject NewGame = MyComplexObject();
        return NewGame;
    }
}

或:

public static MyComplexObject Load(Context context){
    try{
        FileInputStream fis = context.getApplication().openFileInput("player1.data");
        ObjectInputStream is = new ObjectInputStream(fis);
        MyComplexObject Game = (MyComplexObject) is.readObject();
        is.close();
        fis.close();
        return Game;
    }catch (Exception e){
        Log.e("Load", "creating new game - \n" + e.toString());
        MyComplexObject NewGame = MyComplexObject();
        return NewGame;
    }
}