我是Android的新手,可能是一个愚蠢的问题,但请帮助我。我在尝试保存int值时遇到此错误。
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
这是我的代码
SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
String value = "com.example.app.value";
int i = prefs.getInt(value, 0);
写作
prefs.edit().putInt(number, i).apply();
我只想设置一个SharedPreferences,并希望在第一次读取它并将其写入Activity中。你能告诉我如何解决或提出如何制作它的其他建议吗? 感谢
修改
public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "myprefs";
public static final String value = "key";
int i = sharedpreferences.getInt(value, 0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
}
public void sendMessage(View view) {
i += 1;
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt(value, i);
editor.apply();
}
我设法以不同的方式保存pereferences但我无法在MainActivity扩展Activity类中读取它。
日志:Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int android.content.SharedPreferences.getInt(java.lang.String, int)' on a null object reference
答案 0 :(得分:8)
这是SharedPreferences
在SharedPreferences中保存name
:
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString(key, name);
editor.apply();
从SharedPreferences获取name
:
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
String name = sharedPreferences.getString(key, "default value");
有关详细信息,请访问:http://developer.android.com/training/basics/data-storage/shared-preferences.html
更新代码:
public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "myprefs";
public static final String value = "key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
int i = sharedpreferences.getInt(value, 0);
//use the value of i where needed.
}
public void saveMessage(View view) {
i += 1;
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt(value, i);
editor.apply();
}
答案 1 :(得分:1)
你可以像这样实现它
public class PreferenceClass {
public static final String PREFERENCE_NAME = "PREFERENCE_DATA";
private final SharedPreferences sharedpreferences;
public PreferenceClass(Context context) {
sharedpreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
}
public int getCount() {
int count = sharedpreferences.getInt("count", -1);
return count;
}
public void setCount(int count) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt("count", count);
editor.commit();
}
public void clearCount() {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.remove("count");
editor.commit();
}
}
如果没有设置值,getCount()将返回-1。
答案 2 :(得分:0)
这个问题是针对“使用RetroFit简化网络”的android课程。我收到此错误是因为我在Android Manifest中没有我的BaseApplication类...现在一切都很好