How to resolve an error: getSharedPreferences(String, int) is undefined for the type new View.OnClickListener(){}

前端 未结 2 738
走了就别回头了
走了就别回头了 2021-02-06 18:14

I\'m getting this error in my coding and not entirely sure how to resolve this. I\'ve searched to try and resolve this issue but can\'t seem to find anything that works. I\'ve

2条回答
  •  梦谈多话
    2021-02-06 18:55

    Change this code :

    SharedPreferences pref = getSharedPreferences(PREFRENCES_NAME,0);
    

    To :

    SharedPreferences pref = getActivity().getSharedPreferences(PREFRENCES_NAME,0);
    

    Remember you cant call getSharedPreferences method directly from Fragment, because it belongs to the Activity class. Hence, you just need to call getActivity.

提交回复
热议问题