自从我发布了我的一个应用程序的大型更新以来,我收到了数百份来自不同摩托罗拉手机的崩溃报告。堆栈跟踪不通过我的应用程序:
EXCEPTION
java.lang.NullPointerException
at android.content.res.Configuration.updateFrom(Configuration.java:269)
at android.content.res.Resources.updateConfiguration(Resources.java:1257)
at android.app.ActivityThread.handleConfigurationChanged(ActivityThread.java:3701)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1907)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4246)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
at dalvik.system.NativeStart.main(Native Method)
它只发生一次,即用户第一次启动应用程序时。我认为问题来自下面的这段代码。此代码使应用程序在启动时始终使用英语,但随后用户可以选择使用另一种语言,该语言保存在我的 SharedPreferences 中的 Constant.LOCALE 中。程序第一次启动时,“else”子句被执行,问题可能就在那里。但实际上,最奇怪的是,我找不到其他人只对摩托罗拉手机有问题。请注意,它可以在所有其他手机上完美运行。
public static void setCorrectLanguage(final Context context, final SharedPreferences preferences, final Editor editor) {
final Resources resource = context.getResources();
final Configuration cf = resource.getConfiguration();
final String choosenLanguage = preferences.getString(Constant.LOCALE, null);
final DisplayMetrics dm = resource.getDisplayMetrics();
if(choosenLanguage != null) {
cf.locale = new Locale(choosenLanguage);
resource.updateConfiguration(cf, dm);
} else {
cf.locale = new Locale("en");
resource.updateConfiguration(cf, dm);
editor.putString(Constant.LOCALE, "en");
editor.commit();
}
}