英文:
Adding multiple language localization for android not working
问题
根据您的要求,以下是翻译好的内容:
ActivtyRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 在setContentView之前加载Locale
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);
}
FragmentAllRides
// ASYNTASK 从服务器获取数据 /////////////////////////////////////
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);
// 过滤数据
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: 尝试调用虚拟方法 'android.content.res.Resources androidx.fragment.app.FragmentActivity.getResources()',但空对象引用
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)
希望对您有所帮助。
英文:
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 ?
答案1
得分: 0
getActivity()
返回null
防止活动为空的最佳方法是在调用onAttach
时保留活动引用,并在需要的地方使用活动引用。
示例:
@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
}
@Override
public void onDetach() {
super.onDetach();
mContext = null;
}
现在在内部显示Toast:
Toast.makeText(mContext, mContext.getResources().getString(R.string.no_rides_found), Toast.LENGTH_SHORT).show();
编辑:
onAttach(Activity)
已弃用,现在使用onAttach(Context)
。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论