user3165683
user3165683

Reputation: 367

java.lang.NullPointerException using preferencemanager

Logcat error:

01-21 07:23:04.021: E/AndroidRuntime(361): Caused by: java.lang.NullPointerException }

In what i believe to be this piece of java code:

while (date.equals("01:00:00") || bSet);
    int randomNumber = rand.nextInt(ids.length);
    String last = ((getResources().getString(ids[randomNumber])));
    tv.setText(last);
    edit.putString(last, null);
    edit.commit();

i have previously set up preferences as so, just before my oncreate method:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor edit = prefs.edit();

Upvotes: 0

Views: 132

Answers (1)

vipul mittal
vipul mittal

Reputation: 17401

context is not available before onCreate so you cannot use this here:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor edit = prefs.edit();

put this inside onCreate like:

  SharedPreferences prefs;
    SharedPreferences.Editor edit;

and in onCreate

  prefs = PreferenceManager.getDefaultSharedPreferences(this);
  edit = prefs.edit();

Upvotes: 2

Related Questions