Java 空对象引用 Android 上的共享首选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31142325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shared Preferences on a Null Object Reference Android
提问by Simbilli Dev
I am new to android developing maybe its a silly question but please help me. I am getting this error while trying to save an int value.
我是 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
引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)”
And here is my code
这是我的代码
SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
String value = "com.example.app.value";
int i = prefs.getInt(value, 0);
And for write
而对于写
prefs.edit().putInt(number, i).apply();
I just want to set a SharedPreferences and want to read it at the first and write it inside the Activity. How can I solve it?
我只想设置一个 SharedPreferences 并想首先读取它并将其写入活动中。我该如何解决?
EDIT
编辑
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();
}
I managed to save preferences with a different way but I couldn't manage to read it in MainActivity extends Activity class.
我设法以不同的方式保存首选项,但我无法在 MainActivity extends Activity 类中读取它。
log :
日志 :
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int android.content.SharedPreferences.getInt(java.lang.String, int)' on a null object reference
引起:java.lang.NullPointerException: Attempt to invoke interface method 'int android.content.SharedPreferences.getInt(java.lang.String, int)' on a null object reference
采纳答案by Anand Singh
This is an example of SharedPreferences
这是一个例子 SharedPreferences
To save name
in SharedPreferences:
要保存name
在 SharedPreferences 中:
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString(key, name);
editor.apply();
To get name
from SharedPreferences:
name
从 SharedPreferences获取:
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
String name = sharedPreferences.getString(key, "default value");
For more details visit: http://developer.android.com/training/basics/data-storage/shared-preferences.html
更多详情请访问:http: //developer.android.com/training/basics/data-storage/shared-preferences.html
Updated Code:
更新代码:
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();
}
回答by Ashwin S Ashok
You can implement it like this
你可以像这样实现它
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() will return -1 if no value is set.
如果没有设置值,getCount() 将返回 -1。
回答by Saman Arbabi
This question was for the "Simplify Networking with RetroFit" android course. I was getting this error because i didnt have my BaseApplication class in the Android Manifest...Everything is good now
这个问题是针对“使用 RetroFit 简化网络”android 课程的。我收到这个错误是因为我在 Android 清单中没有我的 BaseApplication 类......现在一切都很好