0

I searched for quite some time and to no avail as I still don't know why it's not working.

I am already using SharedPreference between one Activity and another

Code:

Saving Data

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = preferences.edit();

    editor.putString("email", email.getEmail().toString());
    editor.apply();

Retrieving Data

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String userEmail = preferences.getString("email", "defaultValue");

    DisplayEmail.setText(userEmail);

This code is working fine. But now, when moving on to the other shared preference, this happens:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nathan.application/com.example.nathan.application.ActCamera}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2796) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2867) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1570) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:156) at android.app.ActivityThread.main(ActivityThread.java:6595) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832) Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

The code due to this error:

 double Latitude = loc.getLatitude();
            double Longitude = loc.getLatitude();

            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor editor = preferences.edit();


            editor.putString("Country", details.doInBackground().toString());
            editor.putLong("Latitude", Double.doubleToRawLongBits(Latitude));
            editor.putLong("Longitude", Double.doubleToRawLongBits(Longitude));

            editor.apply();


 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    Latitude = Double.longBitsToDouble(preferences.getLong("Latitude", 0));
    Longitude = Double.longBitsToDouble(preferences.getLong("Longitude", 0));
Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
user7943
  • 579
  • 5
  • 19

2 Answers2

0

There have been instances where getDefaultSharedPreferences does not work as expected across different app contexts.

I would recommend you use:

Context mContext = getApplicationContext();
SharedPreferences preferences = mContext.getSharedPreferences("your_shared_preferences_name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
Terrel Lewis
  • 87
  • 1
  • 7
0

Multiple Shared Preference not saving/displaying data

This is called multiprocess app, where you define two or more process in manifest class. There is currently no way of safely accessing SharedPreferences on multiple processes, as described in its documentation.

Solution :

You will use database based library tray instead of SharedPreference if you want work with multiprocess.

https://github.com/grandcentrix/tray

Khemraj Sharma
  • 46,529
  • 18
  • 168
  • 182