-1
SharedPreferences prefs1 = getSharedPreferences("monfichierdeprefs",MODE_PRIVATE);

the same syntax works on a class extend activity but not in extends Fragment So I added context syntax error has disappeared but when I press the button "demande" the application stops the error come from preferance I am sure I may not find the solution could you help me Please!!!!!

public class PageGaucheFragment extends Fragment {

 private static String KEY_SUCCESS = "success";
 private static String KEY_ERROR = "error";
 final String PARA_NOM = "parametre2";
 final String PARA_NOM1 = "parametre3";
 Context context;
 public String fname,lname,ftype;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.page_gauche, container, false);

 /**
 * Action sur bouton demande 
* **/       

Button button = (Button) view.findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        SharedPreferences  prefs1 = context.getSharedPreferences("monfichierdeprefs",Context.MODE_PRIVATE);
        fname = prefs1.getString(PARA_NOM, "");
             lname = prefs1.getString(PARA_NOM1, "");
            ftype="salut"; 
          //apelle de la fonction NetAsync qui verifie la connextion et envoie de demande 
          NetAsync(v);            
      }
    });

    return view;
}
hchems
  • 55
  • 5

1 Answers1

0

Try this..

Change this..

SharedPreferences  prefs1 = context.getSharedPreferences("monfichierdeprefs",Context.MODE_PRIVATE);

to

SharedPreferences  prefs1 = getActivity().getSharedPreferences("monfichierdeprefs",Context.MODE_PRIVATE);

Or

context = getActivity();
Hariharan
  • 24,741
  • 6
  • 50
  • 54