Android Sharedprzypisy
Próbuję odczytać relację SharedPreferences wewnątrz fragmentu. Mój kod jest tym, czego używam, aby uzyskać preferencje w każdej innej aktywności.
SharedPreferences preferences = getSharedPreferences("pref", 0);
I get error
Cannot make a static reference to the non-static method getSharedPreferences(String, int) from the type ContextWrapper
Próbowałem podążać za tymi linkami, ale bez powodzenia uzyskałem dostęp do SharedPreferences za pomocą metod statycznych i Static SharedPreferences . Dziękuję za każde rozwiązanie.
6 answers
Metoda getSharedPreferences
jest metodą obiektu Context
, więc samo wywołanie getSharedPreferences z Fragment
nie zadziała...bo to nie jest kontekst! (Aktywność jest rozszerzeniem kontekstu, więc możemy z niej wywołać getSharedPreferences).
Więc musisz uzyskać kontekst aplikacji przez
// this = your fragment
SharedPreferences preferences = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-14 21:31:06
Zaznaczona odpowiedź nie działa dla mnie, musiałem użyć
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
EDIT:
Lub po prostu spróbuj usunąć this
:
SharedPreferences prefs = getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-26 22:17:10
Dla przypomnienia ta Odpowiedź udzielona przez użytkownika powyżej mnie jest poprawna.
SharedPreferences preferences = this.getActivity().getSharedPreferences("pref",0);
Jednakże, jeśli spróbujesz uzyskać cokolwiek z fragmentu zanim onAttach zostanie wywołany getActivity() zwróci null.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-06 15:54:21
Możesz wykonać SharedPrefences
w onAttach
metodę fragmentu w następujący sposób:
@Override
public void onAttach(Context context) {
super.onAttach(context);
SharedPreferences preferences = context.getSharedPreferences("pref", 0);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-24 09:17:08
This did the trick for me
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
Sprawdź tutaj https://developer.android.com/guide/topics/ui/settings.html#ReadingPrefs
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-24 22:51:11
getActivity()
i onAttach()
nie pomógł mi w tej samej sytuacji
może zrobiłem coś nie tak
ale! Znalazłem inną decyzję
Utworzyłem pole Context thisContext
wewnątrz mojego fragmentu
I otrzymałem bieżący kontekst z metody onCreateView
a teraz mogę pracować ze współdzielonym pref z fragment
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
thisContext = container.getContext();
...
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-26 21:12:37