So what I did is that I created two buttons , and when one is pressed , an intent launches and the locale in the fragment changes. I did that through getting the locale value I desire , convert it to a string and put it in an extra . It worked perfectly fine between activites, but when I set it to a fragment , it gave me an error
(java.lang.RuntimeException: Unable to start activity
ComponentInfo{.phraseDetailActivity}: java.lang.NullPointerException:
language=null,country=,variant=
Here's my code .
The activity sending the extra :
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
String changela = locale.getLanguage();
Intent i = new Intent(Countrylist.this,PhraseDetailActivty.class);
i.putExtra("KEY",changela);
startActivity(i);
}
});
Fragment receiving the locale :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
}
//error at the line under this comment
String changelee = getActivity().getIntent().getStringExtra("KEY");
Locale locale = new Locale(changelee);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getActivity().getResources().updateConfiguration(config,
getActivity().getResources().getDisplayMetrics());
It gives me the error in this line.
String changelee = getActivity().getIntent().getStringExtra("KEY");
I also tried doing lots of stuff like putting the reciving code into another place , putting it in the activity managing the fragment but didnt work .
NOTE : show on my code .
I think the problem is in this line String changelee = getActivity().getIntent().getStringExtra("KEY") i should change something about it.