Getting “cannot resolve method” error when trying to implement getSharedPreferences in Android Studio

后端 未结 2 1810
挽巷
挽巷 2021-02-02 00:59

I\'m trying to create a class KeyValueDB which stores methods for interacting with SharedPreferences, however I\'m running into a problem just defining the class. All I want th

2条回答
  •  遇见更好的自我
    2021-02-02 01:41

    You should pass a context there...

    example of getting a string value:

    public static String getUserName(Context ctx)
    {
        return getSharedPreferences(ctx).getString(PREF_USER_NAME, "");
    }
    

    where PREF_USER_NAME is the key and the second parameter is when it cant find the key it returns ""

提交回复
热议问题