I hope anyone of you can help me.
I make a game like 4Pics1Word.
My problem is I want to load the Level Randomly, so I use a solution which I get from this Question: Android: Check Array for a number Random
public SharedPreference() {
super();
}
// This four methods are used for maintaining favorites.
public static void savePreferences(Context context) {
SharedPreferences settings;
Editor editor;
settings = context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
editor = settings.edit();
System.out.println("FirstTime is " + firstTime);
editor.putInt(Coins, FlagsOfNations.getCoins());
editor.putInt(CurrentQuestion, FlagsOfNations.getCurrentQuestion());
editor.putInt(CurrentImage, FlagsOfNations.getCurrentImage());
editor.putBoolean("FirstTimeCreate", firstTime);
int a = 0;
for (int i = 0; i < 10; i++); {
editor.putInt("list_"+ a, list.get(a));
a++;
System.out.println("Es wurde gespeichert: " + list.get(a));
}
editor.commit();
}
public static void loadPreferences(Context context) {
SharedPreferences settings;
settings = context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
FlagsOfNations.setCoins(settings.getInt(Coins, 400));
FlagsOfNations.setCurrentQuestion(settings.getInt(CurrentQuestion, -1));
FlagsOfNations.setCurrentQuestion(settings.getInt(CurrentImage, -1));
AlertDialogFragment1.setAktuellerButton(settings.getInt("test32", 0));
firstTime = settings.getBoolean("FirstTimeCreate", true);
if (firstTime == true) {
// list in loadpreference und dann in flagsofnation bei start nur load nicht save
for (int i = 0; i < 10; i++) {
list.add(i);
}
Collections.shuffle(list);
// output the generated list
for (int i = 0; i < 10; i++) {
System.out.println("UsedImage: " + list.get(i));
}
firstTime = false;
}
int size = 10;
size = settings.getInt("ListSize", 10);
System.out.println("Size of List: " + size);
int a = 0;
for (int i = 0; i < size; i++); {
list.set(a, settings.getInt("list_"+ a, 0));
System.out.println("Es wurde gespeichert: " + list.get(a));
a++;
}
System.out.println("List asd save: " + list.get(0));
System.out.println("List asd save: " + list.get(1));
System.out.println("List asd save: " + list.get(2));
System.out.println("List asd save: " + list.get(3));
System.out.println("List asd save: " + list.get(4));
System.out.println("List asd save: " + list.get(5));
System.out.println("List asd save: " + list.get(6));
System.out.println("List asd save: " + list.get(7));
System.out.println("List asd save: " + list.get(8));
System.out.println("List asd save: " + list.get(9));
}
If I try it, it work if I close the Game with the Backbutton or the homebutton and start it again it work. But If I click on the (Sorry I don´t know the english word for this button where I can switch to other open apps) and then close the game on the Menu where I can see all open apps and then restart the application I got this error:
03-19 21:57:11.071: E/AndroidRuntime(2989): FATAL EXCEPTION: main
03-19 21:57:11.071: E/AndroidRuntime(2989): Process: com.developer.flagsofnations, PID: 2989
03-19 21:57:11.071: E/AndroidRuntime(2989): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.developer.flagsofnations/com.developer.flagsofnations.FlagsOfNations}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-19 21:57:11.071: E/AndroidRuntime(2989): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
03-19 21:57:11.071: E/AndroidRuntime(2989): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
03-19 21:57:11.071: E/AndroidRuntime(2989): at android.app.ActivityThread.access$800(ActivityThread.java:156)
03-19 21:57:11.071: E/AndroidRuntime(2989): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
I hope you can help me
I got it work with the following code.
public static void savePreferences(Context context) {
SharedPreferences settings;
Editor editor;
settings = context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
editor = settings.edit();
System.out.println("FirstTime is " + firstTime);
editor.putInt(Coins, FlagsOfNations.getCoins());
editor.putInt(CurrentQuestion, FlagsOfNations.getCurrentQuestion());
editor.putInt(CurrentImage, FlagsOfNations.getCurrentImage());
editor.putBoolean("FirstTimeCreate", firstTime);
editor.putString("GsonGson", Test);
editor.commit();
}
public static void loadPreferences(Context context) {
SharedPreferences settings;
settings = context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
FlagsOfNations.setCoins(settings.getInt(Coins, 400));
FlagsOfNations.setCurrentQuestion(settings.getInt(CurrentQuestion, -1));
FlagsOfNations.setCurrentQuestion(settings.getInt(CurrentImage, -1));
AlertDialogFragment1.setAktuellerButton(settings.getInt("test32", 0));
firstTime = settings.getBoolean("FirstTimeCreate", true);
// list in loadpreference und dann in flagsofnation bei start nur
// load nicht save
for (int i = 0; i < 10; i++) {
listTest.add(i);
}
Collections.shuffle(listTest);
// output the generated list
for (int i = 0; i < 10; i++) {
System.out.println("UsedImage: " + listTest.get(i));
}
Gson gson = new Gson();
String jsonList = gson.toJson(listTest);
Log.d("TAG", "jsonList = " + jsonList);
Test = settings.getString("GsonGson", jsonList);
Type type = new TypeToken<ArrayList<Integer>>() {
}.getType();
list = gson.fromJson(Test, type);
Log.d("TAG", "list = " + list);
}
I call this class so in OnCreate in MainActivity:
SharedPreference.loadPreferences(getApplicationContext());
And this in MainActivity to save:
@Override
protected void onStop() {
super.onStop();
SharedPreference.savePreferences(getApplicationContext());
}
@Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null)
mHelper.dispose();
mHelper = null;
SharedPreference.savePreferences(getApplicationContext());
}
I have only one question is it right to call the save Method in onDestroy and OnStop?