Android:sharedPreference在Fragment中,没有从Class中读取数据



我有这个类用于SharedPref工作

package com.box.a100rados.Data;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import static android.content.Context.MODE_PRIVATE;

public class UserAccessSession {
private SharedPreferences sharedPref;
private Editor editor;

private static final String FILTER_RADIUS_DISTANCE      = "FILTER_RADIUS_DISTANCE";
private static final String Name = "Name";
private static final String Photo = "Photo";
private static final String Rank = "Rank";
private static final String Sid = "Sid";
private static final String Ban = "Ban";
private static final String Latitude = "Latitude";
private static final String Longitude = "Longitude";
private static final String SHARED = "UserData";
private static UserAccessSession instance;
public static UserAccessSession getInstance(Context context) {
if(instance == null)
instance = new UserAccessSession(context);
return instance;
}
public UserAccessSession(Context context) {
sharedPref = context.getSharedPreferences(SHARED, MODE_PRIVATE);
editor = sharedPref.edit();
}
public void storeUserSession(UserSession session) {
editor.putString(Name, session.getUsername());
editor.putString(Photo, session.getPhoto_url());
editor.putInt(Rank, session.getUser_Rank());
editor.putString(Sid, session.getUser_id());
editor.putBoolean(Ban, session.getUser_Ban());
editor.putString(Latitude, session.getLatitude());
editor.putString(Longitude, session.getLongitude());
editor.apply();
}
public void clearUserSession() {
editor.putString(Name, null);
editor.putString(Photo, null);
editor.putInt(Rank, 1);
editor.putString(Sid, null);
editor.putBoolean(Ban, false);
editor.putString(Latitude, null);
editor.putString(Longitude, null);
editor.apply();
}
public UserSession getUserSession() {
UserSession session = new UserSession();
session.setUsername( sharedPref.getString(Name, null) );
session.setPhoto_url( sharedPref.getString(Photo, null) );
session.setUser_Rank( sharedPref.getInt(Rank, 1) );
session.setUser_id( sharedPref.getString(Sid, "") );
session.setUser_Ban( sharedPref.getBoolean(Ban, false) );
session.setLatitude(sharedPref.getString(Latitude,"59.946104"));
session.setLongitude(sharedPref.getString(Longitude,"30.306048"));
return session;
}
public void setFilterDistance(float radius) {
editor.putFloat(FILTER_RADIUS_DISTANCE, radius);
editor.commit();
}
public float getFilterDistance() {
return  sharedPref.getFloat(FILTER_RADIUS_DISTANCE, 0);
}
}

如何呼叫:

UserAccessSession tes = UserAccessSession.getInstance(getActivity().getApplicationContext());
UserSession test = tes.getUserSession();

但在test.getLatitude()test.getLongitude()中,我的值为Null。在具有getUser_RankgetUsername或其他值的主活动中有时也会发生同样的情况。我做错了什么?我该如何解决?在地图上,重新打开后,我从SharedPref获得数据,地图相机会移动到我需要的地方。

p.S我添加类似的数据

UserSession userSession = new UserSession();
UserAccessSession session = UserAccessSession.getInstance(MainActivity.this);
String sid = User_SID;
userSession.setUsername(UserName);
userSession.setPhoto_url(Photo_URL);
userSession.setUser_Rank(Rank);
userSession.setUser_id(sid);
session.storeUserSession(userSession);

我这样添加坐标:

userAccess = UserAccessSession.getInstance(MainActivity.this);
userSession = userAccess.getUserSession();
userSession.setLatitude(String.valueOf(location.getLatitude()));
userSession.setLongitude(String.valueOf(location.getLongitude()));
userAccess.storeUserSession(userSession);

感谢您的帮助。我为可能重复的问题我英语说得不好,很难找到信息。

我想当您从不同的来源访问时,会出现问题。

请改用默认的共享首选项,该首选项使用单个文件进行存储,而不是使用不同的文件。

更换此context.getSharedPreferences(SHARED, MODE_PRIVATE);

用这个PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

更改集以获得

public UserSession getUserSession() {
UserSession session = new UserSession();
session.getUsername( sharedPref.getString(Name, null) );
session.getPhoto_url( sharedPref.getString(Photo, null) );
session.getUser_Rank( sharedPref.getInt(Rank, 1) );
session.getUser_id( sharedPref.getString(Sid, "") );
session.getUser_Ban( sharedPref.getBoolean(Ban, false) );
session.getLatitude(sharedPref.getString(Latitude,"59.946104"));
session.getLongitude(sharedPref.getString(Longitude,"30.306048"));
return session;
}

我很愚蠢。

userAccess = UserAccessSession.getInstance(MainActivity.this);
userSession = userAccess.getUserSession();
userSession.setLatitude(String.valueOf(location.getLatitude()));
userSession.setLongitude(String.valueOf(location.getLongitude()));
userAccess.storeUserSession(userSession);

此调用清除了现有数据。将坐标转移到一个单独的方法,一切都正常。

相关内容

  • 没有找到相关文章

最新更新