Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – Adding multiple language localization for android not working

Posted on November 14, 2022

Solution 1 :

getActivity() return null

The best to prevent your activity being null is to keep activity reference when onAttach is called and use the activity reference wherever needed.

Ex:

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    mContext = context;
}

@Override
public void onDetach() {
    super.onDetach();
    mContext = null;
}

And Now inside you show toast as

Toast.makeText(mContext, mContext.getResources().getString(R.string.no_rides_found), Toast.LENGTH_SHORT).show();

Edited:
onAttach(Activity) is depreciated & now onAttach(Context) is being used

Problem :

Following this tutorial I tried to add French language in my app which is currently in English, (Not by changing the system language)

I added the translation in a separate strings file

It seems the problem is in my code. I’m not sure If I have to rewrite the same function in all my activity for it to work here is the code for switching language

Edit 2 : commenting this line fix the crash issue but the language is not updating.
Only the toast messages change to french but the UI layout isn’t

//Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.no_rides_found), Toast.LENGTH_SHORT).show();

ActivtyRide

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //loadLocale before setContentView
    loadLocale();
    setContentView(R.layout.activity_ride);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle(getResources().getString(R.string.app_name));

private void setLocale(String lang) {
    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config  = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
    editor.putString("My_Lang", lang);
    editor.apply();
}
public void loadLocale(){
    SharedPreferences prefs = getSharedPreferences("Settings", Activity.MODE_PRIVATE);
    String language = prefs.getString("My_Lang", "");
    setLocale(language);
}

And here is the Activity that causing me the crash

FragmentAllRides

 //ASYNTASK Getting Data From Server/////////////////////////////////////
public class GetAllRides extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected String doInBackground(String... params) {

    }


    @Override
    protected void onPostExecute(String s) {

        rotateLoading.stop();
        relativeLayout.setVisibility(View.GONE);

        if (server_check) {


            if (server_response.equals("1")) {


                if (driver_name.length > 0) {


                    adapter = new AllRidesAdapter(getActivity(),
                            arrayList);

                    listView.setAdapter(adapter);


                    //filtering data
                    EtSearch.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

                            if (search_value.equals("source")) {
                                adapter.getFilter().filter(charSequence);
                            } else if (search_value.equals("destination")) {
                                adapter.getFilter2().filter(charSequence);
                            } else {
                                Toast.makeText(getActivity(), "ass", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                        }
                    });


                } else {
                    Toast.makeText(getActivity(), server_response_text, Toast.LENGTH_SHORT).show();

                }
                
            } else {

                Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.no_rides_found), Toast.LENGTH_SHORT).show();
            }

        } else {
            Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.server_error), Toast.LENGTH_SHORT).show();

        }
    }
}

Logcat

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources androidx.fragment.app.FragmentActivity.getResources()' on a null object reference
    at com.devqx.covoiturage.Ride.Home.FragmentAllRides$GetAllRides.onPostExecute(FragmentAllRides.java:672)
    at com.devqx.covoiturage.Ride.Home.FragmentAllRides$GetAllRides.onPostExecute(FragmentAllRides.java:464)
    at android.os.AsyncTask.finish(AsyncTask.java:755)

Any ideas ?

READ  [ANSWERED] android - Forgroundservice notification without click event
Powered by Inline Related Posts

Comments

Comment posted by Ishak Benaissa

Thanks this does indeed fix the crash but the language is not updating in the layout

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress