Has anyone tried this?
I have an app that I've configured to run in 2 different languages according to the locale of the device.
Steps to reproduce.
(considering I use French and English)
- Set English locale.
- Load the app.
- Send app to background.
- Change device locale to French.
- Return to your app and make it active.
- (All looks ok.. everything loaded in French now (as it should) )
- Go to another screen.
- Receive
NullPointerException
!!!
HUH?!
Any thoughts on this?
Later edit (request)
Here's the ADB stacktrace:
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: ER开发者_开发问答ROR/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
Anyone?
It might be, that you're out of activity lifecycle and some objects not being created while you're switching, so you should check next:
- If this works w/o step 4 ?
If some values being cached/not translated properly ( like you're setting some fields instead of everytime update)
mField = getString(R.string.title);
Found out what was wrong.
I had a System.Exit(0) on my OnDestroy method within the screen that I was in when going into background.
It seems that if you send the app to background and then come back to it, OnDestroy WON'T be called.
BUT
If you change locale in the meanwhile and then come back to it, OnDestray will be called!!!!
This is weird enough as it is But even more, in my case, onDestroy got called (System.exit(0) implicitly but the screen was still there.. the app didn't exit (even more.. it loaded French correctly !!) this was confusing.
Anyway.. weird stuff solved. Thanks for your input.
Without actually seeing your code it's going to be impossible to tell for certain what the issue is. The comments above give you a great place to start trying to debug the issue. One thing to keep an eye out for would be anytime you use a String as a key. If the key gets translated during the running of the app it will fail to find a value.
If you are making changes to locale at run-time be sure to set the local in onResume(). I has a similar problem with a spinner not keeping the language it was set to when it was changed by the user manually. When the app went to the background the spinner was showing values in English instead of Spanish. I simply set my locale in on resume on the activity.
精彩评论