Logcat错误:
01-21 07:23:04.021: E/AndroidRuntime(361): Caused by: java.lang.NullPointerException }
我相信这段java代码:
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();
我之前已经设置了首选项,就在我的oncreate方法之前:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = prefs.edit();
答案 0 :(得分:2)
onCreate
之前无法使用上下文,因此您无法使用this
这里:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = prefs.edit();
把它放在onCreate里面,如:
SharedPreferences prefs;
SharedPreferences.Editor edit;
和onCreate
prefs = PreferenceManager.getDefaultSharedPreferences(this);
edit = prefs.edit();