سؤال

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();
هل كانت مفيدة؟

المحلول

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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top