Android – how to resolve errors: getsharedpreferences (string, int) does not define type new view. Onclicklistener() {}

I encountered this error in coding and am not completely sure how to solve this problem. I have searched and tried to solve this problem, but I can't seem to find anything useful. I've done this before, but I've never had a fragment, so maybe it's because of that?

I get the following exception:

This is my code:

public class TestingFragment extends Fragment {

public TestingFragment(){}
private Check@R_616_2419@ ch;
private Context pref;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_testing, container, false);

    ch = (Check@R_616_2419@) rootView.findViewById(R.id.check@R_616_2419@62);
    ch.setOnClickListener(new View.OnClickListener() {
        private String PREFRENCES_NAME;

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        if(ch.isChecked())
                {
            SharedPreferences pref = getSharedPreferences(PREFRENCES_NAME,0);
            ch.setChecked(pref.getBoolean("cbx62_ischecked" ,true));
            pref.edit().putBoolean("check",false).commit();
            }
        {
        }}
    });
    return rootView;
} }

Can someone help me with this? Any help will be appreciated!

resolvent:

Change this Code:

SharedPreferences pref = getSharedPreferences(PREFRENCES_NAME,0);

To:

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

Remember, you cannot call the getsharedpreferences method directly from the fragment because it belongs to the activity class. Therefore, you only need to call getactivity

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>