我有一个从设置菜单运行来选择不同语言的方法。 我希望将其保存在 SharedPreferences 文件中,但我不断收到错误且没有任何解释。如果我单击“确定”,应用程序将正常继续,但不会保存所选语言。
方法:
static String langSet = "";
static SharedPreferences languageSettings;
static SharedPreferences.Editor editLang;
public static void updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(locale);
} else {
configuration.locale = locale;
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
创建时:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//LocaleHelper.onCreate(this, "es");
languageSettings = getSharedPreferences("languages", Context.MODE_PRIVATE);
editLang = languageSettings.edit();
if (langSet != ""){
langSet = languageSettings.getString("lang", "");
updateResources(this, langSet);
}
startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(new AdapterView.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
//intent.putExtra("name", friends.get(i));
startActivity(intent);
}
});
}
onCreateOptionsMenu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.english) { // IMPORT THE MENU.XML FOLDER
languageSettings = getSharedPreferences("languages", Context.MODE_PRIVATE);
editLang.putString("lang", "en");
editLang.apply();
langSet = languageSettings.getString("lang", "");
updateResources(this, langSet);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
//intent.putExtra("name", friends.get(i));
startActivity(intent);
}
else if (id == R.id.afrikaans) {
languageSettings = getSharedPreferences("languages", Context.MODE_PRIVATE);
editLang.putString("lang", "af");
editLang.apply();
langSet = languageSettings.getString("lang", "");
updateResources(this, langSet);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
//intent.putExtra("name", friends.get(i));
startActivity(intent);
}
else if (id == R.id.spanish) {
languageSettings = getSharedPreferences("languages", Context.MODE_PRIVATE);
editLang.putString("lang", "es");
editLang.apply();
langSet = languageSettings.getString("lang", "");
updateResources(this, langSet);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
//intent.putExtra("name", friends.get(i));
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
请帮忙。 谢谢
最佳答案
几年前,这件事发生在我身上。错误是当我更改sharedPreferenceType时。
例如,如果将 editLang.putString("lang", "en");
更改为 editLang.putInt("lang", 1);
并且只需安装应用程序,突然它无法工作,因为您对不同类型的值使用了相同的 key 。
为了解决我的问题,需要在安装之前从设备中手动卸载整个应用程序。
因此,我的建议是在进行此类更改之前尝试卸载应用程序或转到 Android 设备中的应用程序详细信息并清除应用程序数据。当然,在应用程序投入生产时要小心,如果您想更改值的类型,我建议您更改键的名称。
关于android - 从设置中保存共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40161517/