CL So
CL So

Reputation: 3759

How to getSharedPreferences in Application?

I tried to call getSharedPreferences in Application...

public class App extends Application{
    public App() {
        SharedPreferences prefs = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    }
}

...but I got a NullPointerException:

java.lang.NullPointerException: Attempt to invoke virtual method
  'android.content.SharedPreferences
  android.content.Context.getSharedPreferences(java.lang.String, int)' 
  on a null object reference

I also tried this and got same exception:

Context con = getApplicationContext();

How can I call getSharedPreferences?

Upvotes: 1

Views: 7397

Answers (7)

Mahesh
Mahesh

Reputation: 2892

You can't create shared preference object in constructor of Application, instead of that create it in onCreate method of application class. Here is chunk of code

public class App extends Application{
    
    public App() {
        
    }
    
    @Override
    public void onCreate(){
        SharedPreferences prefs = getApplicationContext().getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    }
}

Upvotes: 1

dhamini poornachandra
dhamini poornachandra

Reputation: 472

public class Preferences {
    private static Preferences preferences = null;
    private SharedPreferences.Editor editor;
    private String userName;
    private String token;
    private String password;

    private Preferences(Context context) {
        editor = getCurrent(context).edit();
    }

    public static synchronized Preferences getInstance(Context context) {
        if (preferences == null) {
            preferences = new Preferences(context);
        }
        return preferences;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getDefaults(String key, Context context) {
        return getCurrent(context).getString(key, null);
    }

    public void store(String key, String value) {
        editor.putString(key, value);
        editor.apply();
    }

    public void remove(String key) {
        editor.remove(key);
        editor.apply();
    }

    private SharedPreferences getCurrent(Context context) {
        return context.getSharedPreferences("app_preference_key"), Context.MODE_PRIVATE);
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

This is a singleton implementation of preferences.

Upvotes: 0

Manoj Perumarath
Manoj Perumarath

Reputation: 10214

I think atleast you can try like this;

 public class MyApplicationclass extends Application {

Context context;

public MyApplicationclass() {
}

public MyApplicationclass(Context context) {
    this.context = context;
}
}

Now you have the context; So it's easy to create and use sharedpreferences

Upvotes: 0

Jimit Patel
Jimit Patel

Reputation: 4297

Application is the Android class and has it's own life cycle. You cannot initiate or do any other operations related to SharedPreference over there. Because it requires Context and Application's Context has not yet been initialized using it's own life cycle. Thus, it is best practise to initiate anything in onCreate method.

Also one suggestion, make your Application class singleton.

Upvotes: 0

slow_ninja
slow_ninja

Reputation: 11

You are getting a null pointer because you are calling it from the constructor. At that point the application context has not yet been created. Try calling it from onCreate() method of Application class.

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

Override onCreate(), in your app,

@Override
public void onCreate() {
    super.onCreate();

and do it there. Don't forget to declare your Application subclass in the AndroidManifest as well. E.g.

  <application
        android:name="App"

Upvotes: 9

Tishka17
Tishka17

Reputation: 320

Get SharedPreferences object inside onCreate() method, not in constructor

Upvotes: 0

Related Questions