SharedPreferences出错

时间:2017-12-01 05:57:35

标签: android android-sharedpreferences

我前天实施了会话管理代码它工作正常但是现在它在getString()上发出错误请检查以下代码并告诉我哪里出错了。

public class SessionManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "MaangalPref";
private static final String IS_LOGIN = "IsLoggedIn";
public static final String KEY_PASSWORD = "name";
public static final String KEY_EMAIL = "email";
public static final String KEY_GENDER = "gender";
public SessionManager(Context context){
    this._context = context;
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
}

public SessionManager() {}

创建登录会话

public void createLoginSession(String name, String email, String gender){
    editor.putBoolean(IS_LOGIN, true);
    editor.putString(KEY_PASSWORD, name);
    editor.putString(KEY_EMAIL, email);
    editor.putString(KEY_GENDER, gender);
    editor.commit();
}

检查登录方法wil检查用户登录状态 如果为false,它会将用户重定向到登录页面。否则不会做任何事情

public void checkLogin(){
    if(!this.isLoggedIn()){
        Intent i = new Intent(_context, AuthenticActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        _context.startActivity(i);
    }
}

获取存储的会话数据

public HashMap<String, String> getUserDetails(){
    HashMap<String, String> user = new HashMap<String, String>();
    user.put(KEY_PASSWORD, pref.getString(KEY_PASSWORD, null));
    user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
    user.put(KEY_GENDER,pref.getString(KEY_GENDER, null));
    return user;
  }


public void logoutUser(){
    editor.clear();
    editor.commit();
    Intent i = new Intent(_context, AuthenticActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    _context.startActivity(i);
}
public boolean isLoggedIn(){
    return pref.getBoolean(IS_LOGIN, false);
  }
 }

片段类的onCreate方法

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Session class instance
    session = new SessionManager();
    // get user data from session
    HashMap<String, String> user = session.getUserDetails();
    email = user.get(SessionManager.KEY_EMAIL);
    Log.e("email________NewMatches",email);
    DATA_URL = "http://192.168.2.110/xp/new_matches.php?matri_id="+email;
    Log.e("URL________NewMatches",DATA_URL);
}

[1]: https://i.stack.imgur.com/1oSNj.png

1 个答案:

答案 0 :(得分:2)

我认为你在选项卡片段中调用SessionManager的空构造函数,如

session = new SessionManager();

您在SessionManager类中声明

public SessionManager(Context context){
    this._context = context;
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
}

public SessionManager() {}

所以最后在标签片段中使用

 session = new SessionManager(getContext()); //give context of class

而不是 session = new SessionManager(); 使用下面的代码

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Session class instance
    session = new SessionManager(getContext()); //here you have to change
    // get user data from session
    HashMap<String, String> user = session.getUserDetails();
    email = user.get(SessionManager.KEY_EMAIL);
    Log.e("email________NewMatches",email);
    DATA_URL = "http://192.168.2.110/xp/new_matches.php?matri_id="+email;
    Log.e("URL________NewMatches",DATA_URL);
}

并从SessionManager中删除空构造函数以供将来使用....