Как сохранить сеанс в Android через другой фрагмент / активность? - PullRequest
0 голосов
/ 05 ноября 2018

У меня есть этот код для управления сеансом во время входа в систему с помощью SharedPreferences. Код отлично работает во время входа в систему, но когда я использую его в другом фрагменте / деятельности, он больше не работает. Я получаю ошибку «Попытка вызвать виртуальный метод« void android.widget.TextView.setText (java.lang.CharSequence) »для нулевой ссылки на объект».

Может ли кто-нибудь помочь мне правильно вызвать объект управления сеансом? Вот мой код

SessionManagement.java

public class SessionManagement {

//URL to our login.php file
public static final String LOGIN_URL = "http://10.0.2.2:63343/login-test-session/login.php?";

//Keys for email and password as defined in our $_POST['key'] in login.php
public static final String KEY_EMAIL = "email";
public static final String KEY_PASSWORD = "password";

//If server response is equal to this that means login is successful
public static final String LOGIN_SUCCESS = "success";

//Keys for Sharedpreferences
//This would be the name of our shared preferences
public static final String SHARED_PREF_NAME = "themoneygerapp";

//This would be used to store the email of current logged in user
public static final String EMAIL_SHARED_PREF = "email";

//We will use this to store the boolean in sharedpreference to track user is loggedin or not
public static final String LOGGEDIN_SHARED_PREF = "loggedin";

ExpensesView.java (фрагмент)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_expenses_view, container, false);
    profiletest = (TextView) view.findViewById(R.id.txtProfileExpenses);
    SharedPreferences sharedPreferences = getActivity().getApplicationContext().getSharedPreferences(SessionManagement.SHARED_PREF_NAME, Context.MODE_PRIVATE);
    String email = sharedPreferences.getString(SessionManagement.EMAIL_SHARED_PREF,"Not Available");
    profiletest.setText("Expenses for user: " + email);
    loadExpenses();
    return view;
}
...