在应用程序处于后台时更改语言

时间:2011-06-03 21:47:48

标签: android background locale

有没有人试过这个?

我有一个应用程序,我已根据设备的区域设置配置为以2种不同的语言运行。

重现的步骤。

(考虑到我使用法语和英语)

  1. 设置英语区域设置。
  2. 加载应用。
  3. 将应用发送到后台。
  4. 将设备区域设置更改为法语。
  5. 返回您的应用并将其激活。
  6. (所有看起来都很好..现在用法语加载所有东西(应该如此))
  7. 转到另一个屏幕。
  8. 收到NullPointerException !!!
  9. HUH?!

    对此有何想法?

    稍后编辑(请求)

    这是ADB堆栈跟踪:

    06-04 00:35:27.093: WARN/dalvikvm(18434): threadid=3: thread exiting with uncaught exception (group=0x4001dc20)
    06-04 00:35:27.093: ERROR/AndroidRuntime(18434): Uncaught handler: thread main exiting due to uncaught exception
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MyPackageName.__XXXXX__/com.MyPackageName.__XXXXX__.activity.MenuTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MyPackageName.__XXXXX__/com.MyPackageName.__XXXXX__.activitygroup.MyProfileActivityGroup}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MyPackageName.__XXXXX__/com.MyPackageName.__XXXXX__.activity.myprofile.DriverProfileActivity}: java.lang.NullPointerException
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.os.Handler.dispatchMessage(Handler.java:99)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.os.Looper.loop(Looper.java:123)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.ActivityThread.main(ActivityThread.java:4363)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at java.lang.reflect.Method.invoke(Method.java:521)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at dalvik.system.NativeStart.main(Native Method)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MyPackageName.__XXXXX__/com.MyPackageName.__XXXXX__.activitygroup.MyProfileActivityGroup}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MyPackageName.__XXXXX__/com.MyPackageName.__XXXXX__.activity.myprofile.DriverProfileActivity}: java.lang.NullPointerException
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.widget.TabHost.setCurrentTab(TabHost.java:320)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.widget.TabHost.addTab(TabHost.java:213)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at com.MyPackageName.__XXXXX__.activity.MenuTabActivity.onCreate(MenuTabActivity.java:80)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     ... 11 more
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MyPackageName.__XXXXX__/com.MyPackageName.__XXXXX__.activity.myprofile.DriverProfileActivity}: java.lang.NullPointerException
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at com.MyPackageName.__XXXXX__.activitygroup.MyProfileActivityGroup.startChildActivity(MyProfileActivityGroup.java:69)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at com.MyPackageName.__XXXXX__.activitygroup.MyProfileActivityGroup.onCreate(MyProfileActivityGroup.java:63)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     ... 20 more
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434): Caused by: java.lang.NullPointerException
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at com.MyPackageName.__XXXXX__.activity.myprofile.DriverProfileActivity.onCreate(DriverProfileActivity.java:545)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    06-04 00:35:27.123: ERROR/AndroidRuntime(18434):     ... 27 more
    

    任何?

4 个答案:

答案 0 :(得分:1)

可能是,您在活动生命周期之外并且在切换时没有创建某些对象,因此您应该检查下一步:

  1. 如果这不适用于第4步?
  2. 如果某些值被缓存/未正确翻译(就像您设置的是某些字段而不是每次更新)

    mField = getString(R.string.title);
    

答案 1 :(得分:1)

发现错误。

我在进入后台时屏幕上的OnDestroy方法上有一个System.Exit(0)。

似乎如果您将应用程序发送到后台然后再回到它,则不会调用OnDestroy。

BUT

如果你同时改变语言环境然后回到它,OnDestray将被调用!!!!

这很奇怪,但是更多,在我的情况下,onDestroy被隐式调用(System.exit(0)但屏幕仍然存在..应用程序没有退出(更多..它加载法语正确!!)这令人困惑。

无论如何......奇怪的东西解决了。感谢您的投入。

答案 2 :(得分:0)

如果没有真正看到您的代码,就无法确定问题是什么。上面的评论为您提供了一个开始尝试调试问题的好地方。有一点需要注意的是,只要你使用String作为键。如果在应用程序运行期间密钥被翻译,则无法找到值。

答案 3 :(得分:0)

如果要在运行时更改语言环境,请确保在onResume()中设置语言环境。我有一个类似的问题,微调器没有保留用户手动更改时设置的语言。当应用程序进入后台时,微调框显示的是英语而不是西班牙语的值。我只是将我的语言环境设置为该活动的简历。