문제

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