"static" class speichern

  • Antworten:5
Anthony Swofford
  • Forum-Beiträge: 3

13.02.2014, 14:38:43 via Website

Hallo,

Ich bin noch recht neu im App-programmieren und habe gerade folgendes Problem für das ich seit mehren Tagen schon keine Lösung finde.

Ich Schreibe gerade ein Runden basiertes Spiel und habe eine class welche alle relevanten Daten des Spiels als static Variabeln enthält.

Nun möchte ich das nach jedem Zug eines Spielers (onClick) die Spieldaten gespeichert werden, sodass im Falle eines Crashs diese Daten beim Neustart (onCreate) der App wieder geladen werden.

Bei meinem aktuellen Versuch mit einen ObjectOutputStream und ObjectInputStream behalten alle static Variabeln beim Neustart Ihren Defaultwert der class

Hat jemand eine Idee was ich da falsch mache und wie es richtig gehört? oder gibt es vieleicht einen ganz anderen Ansatz der für mein Problem besser passt?

Vielen Dank

Im folgenden Ausschnitte des Code



CSpieldaten
(saveObject + getObject)
1public class CSpieldaten implements Serializable {
2
3 private static final long serialVersionUID = 1L;
4
5 //Aktuelles Spiel
6 /////////////////////////////////////////////////////////////////////////////////////////
7 public static int AktuelleRunde = 0;
8 public static int AktiverSpieler = 0;
9 public static int AktivesDorf = 0;
10 public static int AktivesGebaeude = 0;
11 public static int AktuellerBauplatz = 1;
12 public static int AktuellesTeam = 1;
13 public static int AktuellStaerksterMacht[] = {-1,-1,-1}; //[Händler / Armee / Siedler]
14 public static int SelektiertePositionX = 0;
15 public static int SelektiertePositionY = 0;
16 public static int AktuellesFeld = 1;
17 public static int AktuellesGebaeude = 1;
18 public static int AktuellesGebiet = 0;
19 public static int AktuellerBericht = 0;
20 public static ArrayList<Integer> SpinnerBerichtNr = new ArrayList<Integer>();
21 public static ArrayList<ArrayAdapter<String>> ArrayAdapter = new ArrayList<ArrayAdapter<String>>();
22 public static ArrayList<CBericht> CB_AktuellerBericht = new ArrayList<CBericht>();
23 public static CKosten AktuelleKosten = new CKosten(0,0,0,0,0);
24 public static CGebaeude TempGebaeude = new CGebaeude();
25 public static ArrayList<CSpieler> CS_Spieler = new ArrayList<CSpieler>();
26 public static ArrayList<CWegzoll> CW_Wegzoll = new ArrayList<CWegzoll>();
27 public static CDorf CD_Handelsstadt = new CDorf0
28 public static CDorf CD_Festung = new CDorf0
29 public static CDorf CD_Forschungszentrum = new CDorf0
30 public static CUnterstuetzung CU_FestungVerteidigung = new CUnterstuetzung();
31 public static CUnterstuetzung CU_FestungStartArmee = new CUnterstuetzung();
32
33
34 public boolean saveObject(CSpieldaten obj) {
35 final File suspend_f=new File("/storage/sdcard0/Android/obb/Trav/TravD2");
36
37 FileOutputStream fos = null;
38 ObjectOutputStream oos = null;
39 boolean keep = true;
40
41 try {
42 fos = new FileOutputStream(suspend_f);
43 oos = new ObjectOutputStream(fos);
44 oos.writeObject(obj);
45 } catch (Exception e) {
46 keep = false;
47 } finally {
48 try {
49 if (oos != null) oos.close();
50 if (fos != null) fos.close();
51 if (keep == false) suspend_f.delete();
52 } catch (Exception e) { /* do nothing */ }
53 }
54
55 return keep;
56 }
57
58 public CSpieldaten getObject(Context c) {
59 final File suspend_f=new File("/storage/sdcard0/Android/obb/Trav/TravD2");
60
61 CSpieldaten simpleClass= null;
62 FileInputStream fis = null;
63 ObjectInputStream is = null;
64
65 try {
66 fis = new FileInputStream(suspend_f);
67 is = new ObjectInputStream(fis);
68 simpleClass = (CSpieldaten) is.readObject();
69 } catch(Exception e) {
70 String val= e.getMessage();
71 } finally {
72 try {
73 if (fis != null) fis.close();
74 if (is != null) is.close();
75 } catch (Exception e) { }
76 }
77
78 return simpleClass;
79 }

Aufrufen der Funktionen in der Activity
1protected void onCreate(Bundle savedInstanceState) {
2 super.onCreate(savedInstanceState);
3 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
4 setContentView(R.layout.activity_main);
5
6 CSpieldaten c = new CSpieldaten();
7 c = c.getObject(this);
8
9
10
11 ImageButton IB_Next = (ImageButton) findViewById(R.id.IB_Next);
12 IB_Next.setOnClickListener(new OnClickListener(){
13
14 public void onClick(View v) {
15
16 CSpieldaten c = new CSpieldaten();
17 boolean result = c.saveObject(c);
18 }
19 });
20 }

Antworten
Mac Systems
  • Forum-Beiträge: 1.727

13.02.2014, 15:17:04 via Website

Gebe dir mal die Exceptions alle aus anstatt diese zu ignorieren, sowas einfach zu ignorieren ist nicht gut

Windmate HD, See you @ IO 14 , Worked on Wundercar, Glass V3, LG G Watch, Moto 360, Android TV

Antworten
Anthony Swofford
  • Forum-Beiträge: 3

13.02.2014, 15:32:28 via Website

Hab nun eine Ausgabe der Exceptions hinzugefügt. Allerdings tritt keine Exception auf.
Gehe ich Schritt für Schritt durch den Code speichert und lädt er die Daten angeblich ohne Probleme. Am Ende ist die class jedoch wieder auf den Defaultwerten.

Antworten
impjor
  • Forum-Beiträge: 1.793

13.02.2014, 15:36:21 via App

Warum speicherst du das überhaupt in statischen Klassenvariablen? Entwirf lieber eine "normale" Klasse. Die sollte dann auch korrekt gespeichert werden.

LG

Liebe Grüße impjor.

Für ein gutes Miteinander: Unsere Regeln
Apps für jeden Einsatzzweck
Stellt eure App vor!

Antworten
Anthony Swofford
  • Forum-Beiträge: 3

13.02.2014, 15:43:26 via Website

Muss auf diese Klasse von vielen anderen Klassen und Aktivitys zugreifen und die dort enthaltenen Variablen sollen für alle die gleichen Wert haben

Antworten
Mac Systems
  • Forum-Beiträge: 1.727

13.02.2014, 16:52:03 via Website

Dann mach ein Singleton daraus -> http://de.wikipedia.org/wiki/Singleton_(Entwurfsmuster)
Oder leg es direkt auf dem ApplicationContext ab, was per se ein Singleton ist

Windmate HD, See you @ IO 14 , Worked on Wundercar, Glass V3, LG G Watch, Moto 360, Android TV

Antworten