0

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.

Metalloid66
  • 119
  • 10

1 Answers1

1

Have you tried overriding the onConfigurationChanged() to get the updated Locale?

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    final Locale locale = newConfig.locale;
    ...
}

UPDATE:

Listen to a LocalBroadcastManager and once the locale has changed pass the data via Intent,

private BroadcastReceiver myReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String changela = getResources().getConfiguration().locale.getLanguage()
        Intent i = new Intent(Countrylist.this,PhraseDetailActivty.class);
        i.putExtra("KEY",changela);
        startActivity(i);
    }
};

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());


           IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
           registerReceiver(myReceiver, filter);
        }
    });

UPDATE 2:

From Activity you send data with intent as:

Bundle bundle = new Bundle();
bundle.putString("KEY", changela);
// set YourFragmentclass Arguments
YourFragmentclass fragobj = new YourFragmentclass ();
fragobj.setArguments(bundle);

and in Fragment onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("KEY");    
    return inflater.inflate(R.layout.fragment, container, false);
}
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50