尝试在空对象引用上调用接口方法 sharedPreferences.edit()'
Attempt to invoke interface method sharedPreferences.edit()' on a null object reference
我正在尝试编写一种方法来在我的应用程序中保存每辆自行车的维护日志。该方法循环遍历每辆自行车,在内部循环遍历每个维护日志并保存。
我可以看到正确的信息,因为每个点都放置了 Log.i。
我的方法的完整代码 -
public static void saveLogs() {
for (Bike thisBike : bikes) {
Log.i("Saving Logs", "" + thisBike);
try {
ArrayList<String> dates = new ArrayList<>();
ArrayList<String> logs = new ArrayList<>();
ArrayList<String> costs = new ArrayList<>();
for (maintenanceLogDetails thisLog : thisBike.maintenanceLogs) {
Log.i("Date :", thisLog.date);
dates.add(thisLog.date);
Log.i("Log :", thisLog.log);
logs.add(thisLog.log);
Log.i("Cost :", Double.toString(thisLog.price));
costs.add(Double.toString(thisLog.price));
}
sharedPreferences.edit().putString("dates", ObjectSerializer.serialize(dates)).apply();
sharedPreferences.edit().putString("logs", ObjectSerializer.serialize(logs)).apply();
sharedPreferences.edit().putString("costs", ObjectSerializer.serialize(costs)).apply();
} catch (Exception e) {
e.printStackTrace();
Log.i("Adding details", "Failed attempt");
}
}
}
在 class 的顶部我有 -
static SharedPreferences sharedPreferences;
我得到的完整错误是 -
Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
在 -
这一行
sharedPreferences.edit().putString("dates", ObjectSerializer.serialize(dates)).apply();
让我感到困惑的是,我几乎从其他地方复制了代码。
我做错了什么?
非常感谢!
static SharedPreferences sharedPreferences;
你有没有实例化它..??
getContext().getSharedPreferences(String, int)
https://developer.android.com/reference/android/content/SharedPreferences.html
我正在尝试编写一种方法来在我的应用程序中保存每辆自行车的维护日志。该方法循环遍历每辆自行车,在内部循环遍历每个维护日志并保存。
我可以看到正确的信息,因为每个点都放置了 Log.i。
我的方法的完整代码 -
public static void saveLogs() {
for (Bike thisBike : bikes) {
Log.i("Saving Logs", "" + thisBike);
try {
ArrayList<String> dates = new ArrayList<>();
ArrayList<String> logs = new ArrayList<>();
ArrayList<String> costs = new ArrayList<>();
for (maintenanceLogDetails thisLog : thisBike.maintenanceLogs) {
Log.i("Date :", thisLog.date);
dates.add(thisLog.date);
Log.i("Log :", thisLog.log);
logs.add(thisLog.log);
Log.i("Cost :", Double.toString(thisLog.price));
costs.add(Double.toString(thisLog.price));
}
sharedPreferences.edit().putString("dates", ObjectSerializer.serialize(dates)).apply();
sharedPreferences.edit().putString("logs", ObjectSerializer.serialize(logs)).apply();
sharedPreferences.edit().putString("costs", ObjectSerializer.serialize(costs)).apply();
} catch (Exception e) {
e.printStackTrace();
Log.i("Adding details", "Failed attempt");
}
}
}
在 class 的顶部我有 -
static SharedPreferences sharedPreferences;
我得到的完整错误是 -
Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
在 -
这一行sharedPreferences.edit().putString("dates", ObjectSerializer.serialize(dates)).apply();
让我感到困惑的是,我几乎从其他地方复制了代码。
我做错了什么?
非常感谢!
static SharedPreferences sharedPreferences;
你有没有实例化它..??
getContext().getSharedPreferences(String, int)
https://developer.android.com/reference/android/content/SharedPreferences.html