2014-03-05 44 views
0

我試圖保存我的應用程序的遊戲狀態,以便重新啓動應用程序後可以重新加載。我試圖保存的對象非常複雜,並且包含大量數據。我一直在尋找不同的解決方案,並嘗試了很多。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

​​
+1

大量的靜態變量的!!!,想想你的RAM的負載。 – Kedarnath

回答

0

我其實只是走過去在C姑娘。 Android有一個內置的自動保存功能,應該保存您的數據退出...問:你如何關閉/退出應用程序?我問這是因爲我相信如果你不使用sqlLIte存儲過程,數據存儲在你的RAM中......如果是這種情況......你退出你的應用的方式很重要,可能會刪除你的數據......你可以嘗試和保存所有的數據,當用戶失去焦點

if blah.lostfocus 
yourData .store value 

你或許應該使用持久存儲的方法在應用程序崩潰的情況下,你所提到的。

檢查此鏈接,告訴你如何 - Here

+0

我通過任務管理器關閉它,因爲應用程序崩潰後還應該能夠重新加載數據。 – user3383106

+0

這就是爲什麼你丟失你的數據。你將需要一個持久的存儲方法。 – JayD

+0

我也試過ObjectOutputStream。但我有同樣的問題。但是,這不應該是一種持久的存儲方法。 – user3383106