0
votes

I'm passing a string between activities with sharedPref. I'm trying to make my code more effective, and I have a wierd problem that I can't figure out.

My code in MainActivity.class :

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    //if statement for the first run of the app, else statement uses the defined city.
    if (SharedPref.getCity().equals("")) {

    } else {
        cityText = (TextView) findViewById(R.id.prefCity);
        cityText.setText(SharedPref.getCity());
    }

my SharedPref.java :

public class SharedPref {
private static SharedPreferences mSharedPreferences;

//sharedPref Manager.
public SharedPref(final Context context) {
    mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

}

public static void setCity(String item) {
    mSharedPreferences.edit().putString("city", item).apply();
}

public static String getCity() {
    return mSharedPreferences.getString("city", "");
}

}

as you can see, in my MainActivity.class I am accessing the data thru "SharedPref.getCity()". meaning directly from the class, that's why I used static methods. yet the app crashes. wierd thing is, if I initialize my sharedpref in MainActivity like this:

SharedPref sharedPref = new SharedPref(getBaseContext());

then the app works just fine, even tho sharedPref is a variable that isn't used even once!

what am I missing?

p.s here's my logcat pic : logcat

Thank you in advance for your help.

3
i think because SharedPref.getCity() return null, try to change "if (SharedPref.getCity().equals(""))" to "if (SharedPref.getCity()== null )"Rami
Any particular reason you are passing using sharedPrefs and not intents?Galax

3 Answers

1
votes

Initially the shared preference is not even containing var "city" so initially the condition should be sharepref.contains("city") then check it's value in further iteration.

0
votes

in your setCity method you use the mSharedPreferences which is not initialized yet.

you need to create a new instance of your SharedPref class and make setCity none static.

or if you want the setCity to be static you can do this :

public static void setCity(Context context, String item) {
    if(mSharedPreferences == null)
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

    mSharedPreferences.edit().putString("city", item).apply();
}
0
votes

Take a look at this code:

public static final String SETTINGS_NAME = "MY_SETTINGS";
public static final String FB_TOKEN = "FB_TOKEN";
public static final String LANGUAGE = "LANGUAGE";

public static boolean writeString(Context context, String name, String value)
{
    SharedPreferences settings = context.getSharedPreferences(SETTINGS_NAME, 0);
    Editor editor = settings.edit();
    editor.putString(name, value);

    return editor.commit();
}

public static boolean writeInt(Context context, String name, int value)
{
    SharedPreferences settings = context.getSharedPreferences(SETTINGS_NAME, 0);
    Editor editor = settings.edit();
    editor.putInt(name, value);

    return editor.commit();
}

public static String readString(Context context, String name, String defValue)
{
    SharedPreferences settings = context.getSharedPreferences(SETTINGS_NAME, 0);
    return settings.getString(name, defValue);
}

public static int readInt(Context context, String name, int defValue)
{
    SharedPreferences settings = context.getSharedPreferences(SETTINGS_NAME, 0);
    return settings.getInt(name, defValue);
}

Note that "SETTINGS_NAME" is the name of your schema. the other static names are just to make it easier.