我試圖保存我的應用程序的遊戲狀態,以便重新啓動應用程序後可以重新加載。我試圖保存的對象非常複雜,並且包含大量數據。我一直在尋找不同的解決方案,並嘗試了很多。Android - 應用程序關閉後保存和加載對象的問題
我得到的最接近的是,應用程序能夠在運行時保存和加載數據。 關閉並重新啓動應用程序時,它有時也會成功加載數據,但只是在非常罕見的時刻。
有沒有人知道問題可能是什麼?
的Object
我試圖挽救如下所示(我離開了不重要的功能):
public class SCSpieldaten implements Serializable {
private static final long serialVersionUID = 1L;
//Aktuelles Spiel
/////////////////////////////////////////////////////////////////////////////////////////
public static Random rand = new Random();
public static int EinTabletBedienung = 1;
public static int KartePortLand = 0;
public static int AktuelleRunde = 0;
public static int AktiverSpieler = 0;
public static int AktivesDorf = 0;
public static int AktuellerBauplatz = 1;
public static int AktuellStaerksterMacht[] = {KEINEANGABE,KEINEANGABE,KEINEANGABE};
public static int SelektiertePositionX = 0;
public static int SelektiertePositionY = 0;
public static int AktuellesFeld = 1;
public static int AktuellesGebaeude = 1;
public static int AktuellesGebiet = 0;
public static int AktuelleAnzahlUnit[] = {0,0,0};
public static int AktuellerEinheitentyp[] = {KEINEANGABE,KEINEANGABE,KEINEANGABE};
public static int AktuellerBericht = 0;
public static ArrayList<Integer> SpinnerBerichtNr = new ArrayList<Integer>();
public static ArrayList<ArrayAdapter<String>> ArrayAdapter = new ArrayList<ArrayAdapter<String>>();
public static ArrayList<CBericht> CB_AktuellerBericht = new ArrayList<CBericht>();
public static CKosten AktuelleKosten = new CKosten(0,0,0,0,0);
public static CGebaeude TempGebaeude = new CGebaeude();
public static ArrayList<CSpieler> CS_Spieler = new ArrayList<CSpieler>();
public static ArrayList<CNeutralesLager> CN_NeutralesLager = new ArrayList<CNeutralesLager>();
public static ArrayList<CWegzoll> CW_Wegzoll = new ArrayList<CWegzoll>();
public static CDorf CD_Handelsstadt = new CDorf(GEBIETNEUTRAL);
public static CDorf CD_Festung = new CDorf(GEBIETNEUTRAL);
public static CDorf CD_Forschungszentrum = new CDorf(GEBIETNEUTRAL);
public static CUnterstuetzung CU_FestungVerteidigung = new CUnterstuetzung();
public static CUnterstuetzung CU_FestungStartArmee = new CUnterstuetzung();
public static Integer[][] SpielerStatistic = new Integer[6][100];
public void save(Context c) throws IOException
{
FileOutputStream fos = c.openFileOutput("test0.txt", Context.MODE_WORLD_WRITEABLE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(this);
os.close();
}
public SCSpieldaten load(Context c) throws IOException, ClassNotFoundException
{
FileInputStream fis = c.openFileInput("test0.txt");
ObjectInputStream is = new ObjectInputStream(fis);
SCSpieldaten simpleClass = (SCSpieldaten) is.readObject();
is.close();
return simpleClass;
}
我使用ObjectOutputStream也試過,但重新啓動應用程序時,它好像得了文件deleatet
大量的靜態變量的!!!,想想你的RAM的負載。 – Kedarnath