我在使用Android 7.0的三星Galaxy S8和S8 Plus设备上遇到了奇怪的崩溃(来自Fabric.io日志)。我尝试验证新创建的Locale (en_US)
,但我得到了ExceptionInInitializerError
。
public class MyApp extends MultiDexApplication {
// Save the original locale for logs and other stuff
private Locale mOriginalLocale;
@Override
public void onCreate() {
// The current locale is en_US
mOriginalLocale = Locale.getDefault();
// Update the user's locale
updateLocale();
}
private void updateLocale() {
// Retrieve the user's saved settings from app. For e.g: en_US
String country = getSettings().getCountry();
String language = getSettings().getLanguage();
// Create the new locale
Locale locale = new Locale(language, country);
// Check if the locale is valid
boolean isValidLocale = LocaleUtils.isAvailableLocale(locale);
// If it's not a valid locale, then remove the country, use only the language
if (!isValidLocale) {
locale = new Locale(language);
// Validate again
if (!LocaleUtils.isAvailableLocale(locale)) {
// Still not a valid locale, fallback to original locale
locale = getDefaultLocale();
}
}
// Update the device's locale
Locale.setDefault(locale);
Resources res = app.getResources();
Configuration configuration = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(locale);
}
else {
configuration.locale = locale;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
app.createConfigurationContext(configuration);
} else {
res.updateConfiguration(configuration, null);
}
}
}
使用以下stacktrace调用boolean isValidLocale = LocaleUtils.isAvailableLocale(locale);
后代码崩溃:
java.lang.ExceptionInInitializerError
at sun.util.LocaleServiceProviderPool.getAllAvailableLocales(LocaleServiceProviderPool.java:203)
at java.util.Locale.getAvailableLocales(Locale.java:1004)
at org.apache.commons.lang3.a$a.<clinit>(LocaleUtils.java:324)
at org.apache.commons.lang3.a.a(LocaleUtils.java:222)
at org.apache.commons.lang3.a.a(LocaleUtils.java:247)
我不知道该怎么做。如果设备当前区域设置已经en_US
,为什么无法通过apache
的实用方法验证?他们的图书馆里有个bug吗?你能给我什么选择?