xml中的字符串标记在16以上的api中创建了一些问题
我正在制作一个两种语言英语和乌尔都语的应用程序。 我有英文字符串文件,我的字符串名称为When_undressing_ar
بِسْمِ اللهِ
并为urdu语言分配urdu字符串xml,并将字符串标记为
بِسْمِ اللهِ
并通过调用从字符串中获取这些值
private static String getDuwaElement(String nameame, Context context) { int duwaId = context.getResources().getIdentifier(name, "string", context.getPackageName()); return context.getString(duwaId); }
当语言设置为英语时,name为“When_undressing_ar”。 当语言设置为urdu时,名称为“لباس_اتارتے_وقت_کیا_پڑھیں_ar”
这一切都很好,给我所有的语言资源,
但是,当我在api 22上尝试这个应用程序时,它会给出exception,即找不到资源。 这对api 16来说效果很好。但是当在高于16的api上运行时会出现exception。 请帮助导致此问题的原因,它提供资源未找到的exception
全面的演示
主要活动
public class MainActivity extends AppCompatActivity { Context context; TextView textView; SharedPreferences preferences; String[] SuppertedLangCodes = {"en", "ur"}; String key = "lang"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; preferences = getSharedPreferences("myprefs", MODE_PRIVATE); textView = (TextView) findViewById(R.id.foodName); textView.setText(getResources().getText(R.string.txt_name)); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (preferences.getString("lang", "").toString().equalsIgnoreCase("") || preferences.getString("lang", "").toString().equalsIgnoreCase(SuppertedLangCodes[0])) { applyLanguage(context, SuppertedLangCodes[1]); preferences.edit().putString(key, SuppertedLangCodes[1]).apply(); } else { applyLanguage(context, SuppertedLangCodes[0]); preferences.edit().putString(key, SuppertedLangCodes[0]).apply(); } } }); } public void applyLanguage(Context context, String language) { android.content.res.Configuration config = new android.content.res.Configuration(); // Since API level 17 or below cause issues with RTL, lets keep them LTR if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) { config.locale = new Locale("en"); } else { config.locale = new Locale(language); } context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); recreate(); } }
activity_main:
Strings.xml正常
Gallery Test Photo Gallery what to pray while undressing
来自ur文件夹的Strings.xml
لباس_اتارتے_وقت_کیا_پڑھیں