Gishantha Darshana
Gishantha Darshana

Reputation: 163

How to use Shared Preferences?

I am new to android..and I'm developing an application using Json. I want to pass a string value from my main activity to another class without starting the activity class. I heard there is some way using shared preferences. I just tried.. but didn't worked..i got null point exception.. here is my code...

confirm = check.AuthenticateUser(name, passwd);  
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name",confirm);
editor.commit();

here is the place i want to send the value of "confirm" (string variable)

and below code shows where i want to get that preference..actually that class named "ShortList".

here is the code where i trying to get the value

public class ShortList extends Activity  {

//ArrayList<HashMap<String, String>> arl;

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  String cargivr = preferences.getString("Name","");

please some one help me..

Upvotes: 0

Views: 242

Answers (4)

laalto
laalto

Reputation: 152927

public class ShortList extends Activity  {

//ArrayList<HashMap<String, String>> arl;

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  String cargivr = preferences.getString("Name","");

Here it seems you're initializing member variables.

You cannot use an Activity as a Context until onCreate() in the activity lifecycle. Member variable initialization is too early and will usually lead to NPE in getBaseContext().

Move the initialization of preferences and cargivr to onCreate() or later.

Upvotes: 0

user3059993
user3059993

Reputation: 344

MainActivity.java

SharedPreferences pref = getApplicationContext().getSharedPreferences("New",Context.MODE_PRIVATE);
Editor edit = pref.edit();
edit.putString("SomeKey", "SomeValue");
edit.commit();

SecondActivity.java

SharedPreferences prefs = getApplicationContext().getSharedPreferences("New",Context.MODE_PRIVATE);
String value = prefs.getString("SomeKey","DefaultValue");

Also make sure that you have added the second activity details in your android manifest file -

<activity
        android:name="com.example.projectName.SecondActivity"
        android:parentActivityName="com.example.sharedpref.MainActivity">        
</activity>

Upvotes: 1

Md. Monsur Hossain Tonmoy
Md. Monsur Hossain Tonmoy

Reputation: 11085

public static String CREDENTIALS_FILENAME = "com.myapp.credentials";
public static String PIN = "pin";

public static void writeCredentials(Context c,String pin) {

        SharedPreferences credentialsPref = c.getSharedPreferences(CREDENTIALS_FILENAME, 0);
        SharedPreferences.Editor editor = credentialsPref.edit();
        editor.putString(PIN, pin);
        editor.commit();

    }

public static String readCredentials(Context c,String pin) {

        SharedPreferences credentialsPref = c.getSharedPreferences(CREDENTIALS_FILENAME, 0);
        SharedPreferences.Editor editor = credentialsPref.edit();
        return credentialsPref.getString(PIN, "default value"); 

    }

Upvotes: 1

Looking Forward
Looking Forward

Reputation: 3585

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ShortList.this);
String cargivr = preferences.getString("Name","");

Upvotes: 0

Related Questions