У меня есть класс с именем SharePrefManager. Вот код пакета com.example.lms02;
public class SharedPrefManager {
private static final String SHARED_PREF_NAME = "volleyregisterlogin";
private static final String KEY_USERNAME = "keyusername";
private static final String KEY_EMAIL = "keyemail";
private static final String KEY_GENDER = "keygender";
private static final String KEY_ID = "keyid";
private static final String KEY_STDID = "keystdid";
private static final String KEY_USER_STATUS = "keyuserstatus";
private static SharedPrefManager mInstance;
private static Context ctx;
private SharedPrefManager(Context context) {
ctx = context;
}
public static synchronized SharedPrefManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new SharedPrefManager(context);
}
return mInstance;
}
//this method will store the user data in shared preferences
public void userLogin(User user) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(KEY_ID, user.getId());
editor.putString(KEY_STDID,user.getStdid());
editor.putString(KEY_USERNAME, user.getName());
editor.putString(KEY_EMAIL, user.getEmail());
editor.putString(KEY_GENDER, user.getGender());
editor.putString(KEY_USER_STATUS, user.getStatus());
editor.apply();
}
//this method will checker whether user is already logged in or not
public boolean isLoggedIn() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(KEY_USERNAME, null) != null;
}
//this method will give the logged in user
public User getUser() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return new User(
sharedPreferences.getInt(KEY_ID, -1),
sharedPreferences.getString(KEY_STDID, null),
sharedPreferences.getString(KEY_USERNAME, null),
sharedPreferences.getString(KEY_EMAIL, null),
sharedPreferences.getString(KEY_GENDER, null),
sharedPreferences.getString(KEY_USER_STATUS, null)
);
}
//this method will logout the user
public void logout() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
ctx.startActivity(new Intent(ctx, LoginActivity.class));
}
}
Я использую фрагменты. Вот код моего фрагмента
public class HomeFragment extends Fragment {
TextView txtvwuserid;
User user = SharedPrefManager.getInstance(getContext()).getUser();
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
txtvwuserid = getActivity().findViewById(R.id.txtvwstdid);
txtvwuserid.setText("Hy");
}
}
Проблема возникает в HomeFragment здесь User user = SharedPrefManager.getInstance (getContext ()). GetUser ();
Показанная ошибка: java .lang.NullPointerException: попытка вызвать виртуальный метод 'android .content.SharedPreferences android .content.Context.getSharedPreferences (java .lang.String, int)' для ссылки на пустой объект
Моя проблема в том, как я могу получить доступ к значениям SharedPrefManager и что означает ошибка