-1

I implemented code for Session Management day before yesterday It's working properly but now It's giving an error on getString() please check this below code and tell me where I am wrong.

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() {}

Create login session

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();
}

Check login method wil check user login status If false it will redirect user to login page Else won't do anything

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);
    }
}

Get stored session data

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 method of Fragment Class

@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

pb007
  • 153
  • 1
  • 5
  • 13

1 Answers1

2

I think your are calling empty constructor of SessionManager in tab fragment like

session = new SessionManager();

that you are declared in your SessionManager class

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

public SessionManager() {}

so finally use in tab fragment

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

instead of session = new SessionManager(); use below code

    @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);
}

and also remove empty constructor from SessionManager for future purpose....

Omkar
  • 3,040
  • 1
  • 22
  • 42
  • can not resolve this on Line (session = new SessionManager(this);) – pb007 Dec 01 '17 at 06:28
  • ok you are right you are in fragment so use **getApplicationContext()** instead of **this** – Omkar Dec 01 '17 at 06:29
  • Okey I Used getContext() insted of getApplicationContext() and working for me – pb007 Dec 01 '17 at 06:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160270/discussion-between-user6734679-and-omi). – pb007 Dec 01 '17 at 09:26