title: spinner localization inside a fragment
body: <p>I edited to have the last code I am using with the answer from Krunal. I am still getting crash when I select one of the spinner item that has a listener.</p>
  
  <p>I want the spinner in my fragment to make the user be able to change the language.</p>
  
  <p>However, when the user changes the language the application crashes... So the problem should come from the listener of my spinner. My spinner display effectively the different items</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public class GreetingsFragment extends Fragment {
  
  public GreetingsFragment(){}
  private Spinner spinner1 ;
  Context thiscontext;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
  
      thiscontext = container.getContext();
      View rootView = inflater.inflate(R.layout.fragment_greetings, container, false);
  
      setSpinnerContent( rootView );
  
  spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
  
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; id, View rootView,
                  int pos, long arg3) {
  
                      if (pos == 1) {
  
                          Toast.makeText(getActivity().getApplicationContext(),
                                  "You have selected Spanish", Toast.LENGTH_SHORT)
                                  .show();
                          setLocale("sp");
                      } else if (pos == 2) {
  
                          Toast.makeText(getActivity().getApplicationContext(),
                                  "You have selected Japanese", Toast.LENGTH_SHORT)
                                  .show();
                          setLocale("jp");
                      } else if (pos == 3) {
  
                          Toast.makeText(getActivity().getApplicationContext(),
                                  "You have selected German", Toast.LENGTH_SHORT)
                                  .show();
                          setLocale("de");
                      }
  
                  }
  
           @Override
           public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
               // TODO Auto-generated method stub
               Toast.makeText(getActivity().getApplicationContext(), "Nothing to select", Toast.LENGTH_LONG).show();
           }
  
              });
  
  
      return rootView;
  
  }
  
   private void setSpinnerContent( View rootView )
      {
        spinner1 = (Spinner) rootView.findViewById( R.id.languages_spinner );
        ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(getActivity(), R.array.LANG, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter( adapter );
      }
  
  
  public void setLocale(String lang) {
  
      Locale myLocale = new Locale(lang);
      Resources res = getResources();
      DisplayMetrics dm = res.getDisplayMetrics();
      Configuration conf = res.getConfiguration();
      conf.locale = myLocale;
      res.updateConfiguration(conf, dm);
      Intent refresh = new Intent(getActivity(), GreetingsFragment.class);
      refresh.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      startActivity(refresh);
  }
  
  }
  </code></pre>