Reputation: 300
Problem solved.
The shared preference should contains anything before I close it for the first time but the error says IndexOutOfBoundsExceptions. How come?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences pref = getSharedPreferences("myfile", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.commit();
if(pref.contains("key")){
List.addAll(pref.getStringSet("key", null));
};
}
@Override
public void onStop(){
super.onStop();
if(size>0){
Set<String> set1 = new HashSet<String>();
set1.addAll(List);
SharedPreferences pref = getSharedPreferences("myfile", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putStringSet("key", set1);
editor.commit();
};
}
I'm not sure what to look for at logcat so I just paste it all.
06-05 13:05:30.023: E/AndroidRuntime(1625): FATAL EXCEPTION: main 06-05 13:05:30.023: E/AndroidRuntime(1625): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vocabulary/com.example.vocabulary.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 06-05 13:05:30.023: E/AndroidRuntime(1625): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 06-05 13:05:30.023: E/AndroidRuntime(1625): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 06-05 13:05:30.023: E/AndroidRuntime(1625): at android.app.ActivityThread.access$600(ActivityThread.java:130) 06-05 13:05:30.023: E/AndroidRuntime(1625): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 06-05 13:05:30.023: E/AndroidRuntime(1625): at android.os.Handler.dispatchMessage(Handler.java:99) 06-05 13:05:30.023: E/AndroidRuntime(1625): at android.os.Looper.loop(Looper.java:137) 06-05 13:05:30.023: E/AndroidRuntime(1625): at android.app.ActivityThread.main(ActivityThread.java:4745) 06-05 13:05:30.023: E/AndroidRuntime(1625): at java.lang.reflect.Method.invokeNative(Native Method) 06-05 13:05:30.023: E/AndroidRuntime(1625): at java.lang.reflect.Method.invoke(Method.java:511) 06-05 13:05:30.023: E/AndroidRuntime(1625): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 06-05 13:05:30.023: E/AndroidRuntime(1625): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 06-05 13:05:30.023: E/AndroidRuntime(1625): at dalvik.system.NativeStart.main(Native Method) 06-05 13:05:30.023: E/AndroidRuntime(1625): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 06-05 13:05:30.023: E/AndroidRuntime(1625): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 06-05 13:05:30.023: E/AndroidRuntime(1625): at java.util.ArrayList.get(ArrayList.java:304) 06-05 13:05:30.023: E/AndroidRuntime(1625): at com.example.vocabulary.MainActivity.PrintWords(MainActivity.java:70) 06-05 13:05:30.023: E/AndroidRuntime(1625): at com.example.vocabulary.MainActivity.ShitsToDoWhenCodeSucks(MainActivity.java:118) 06-05 13:05:30.023: E/AndroidRuntime(1625): at com.example.vocabulary.MainActivity.onCreate(MainActivity.java:145) 06-05 13:05:30.023: E/AndroidRuntime(1625): at android.app.Activity.performCreate(Activity.java:5008) 06-05 13:05:30.023: E/AndroidRuntime(1625): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 06-05 13:05:30.023: E/AndroidRuntime(1625): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 06-05 13:05:30.023: E/AndroidRuntime(1625): ... 11 more
Upvotes: 0
Views: 219
Reputation: 5731
Do you have a preferences file named 'myfile' there?
If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).
Upvotes: 0