So I am in the process of localizing my app for different languages, starting with French, and have come across a bizarre problem.
Upon creating an activity, I call getActionBar().setDisplayHomeAsUpEnabled(true);
. This works fine when testing on my Nexus 7 in both English and French, and works fine on my Nexus 4 when using the default (English) resources.
However, if I try to start the activity in question on my Nexus 4 in French (i.e. the phone is set to French, though the problem also occurs if I try to force the app into French regardless), the app crashes, with Eclipse telling me there was a null pointer exception. I determined the problem to be the setDisplayHomeAsUpEnabled()
call.
Passing false
instead of true
also crashes. Calling getActionBar()
without making any further calls does not crash the app. The app is set up according to a Master-Detail flow (so the core of the app are two Activity-Fragment pairs, one List and one Detail), which I assume is why the Nexus 7 version can work fine despite the Nexus 4 version crashing.
I have double-checked my values
and values-fr
folders, and I am sure there are no missing or extra resources in values-fr
.
Any clues as to why this might be happening?
Here is what LogCat tells me:
04-27 15:13:44.968: D/AndroidRuntime(4244): Shutting down VM
04-27 15:13:44.968: W/dalvikvm(4244): threadid=1: thread exiting with uncaught exception (group=0x41429930)
04-27 15:13:44.978: E/AndroidRuntime(4244): FATAL EXCEPTION: main
04-27 15:13:44.978: E/AndroidRuntime(4244): java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.hachesoftware.soilbearingcapacity/ca.hachesoftware.soilbearingcapacity.ScreenDetailActivity}: java.lang.NullPointerException
04-27 15:13:44.978: E/AndroidRuntime(4244): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-27 15:13:44.978: E/AndroidRuntime(4244): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-27 15:13:44.978: E/AndroidRuntime(4244): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-27 15:13:44.978: E/AndroidRuntime(4244): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-27 15:13:44.978: E/AndroidRuntime(4244): at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 15:13:44.978: E/AndroidRuntime(4244): at android.os.Looper.loop(Looper.java:137)
04-27 15:13:44.978: E/AndroidRuntime(4244): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-27 15:13:44.978: E/AndroidRuntime(4244): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 15:13:44.978: E/AndroidRuntime(4244): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 15:13:44.978: E/AndroidRuntime(4244): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-27 15:13:44.978: E/AndroidRuntime(4244): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-27 15:13:44.978: E/AndroidRuntime(4244): at dalvik.system.NativeStart.main(Native Method)
04-27 15:13:44.978: E/AndroidRuntime(4244): Caused by: java.lang.NullPointerException
04-27 15:13:44.978: E/AndroidRuntime(4244): at ca.hachesoftware.soilbearingcapacity.ScreenDetailActivity.onCreate(ScreenDetailActivity.java:34)
04-27 15:13:44.978: E/AndroidRuntime(4244): at android.app.Activity.performCreate(Activity.java:5104)
04-27 15:13:44.978: E/AndroidRuntime(4244): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-27 15:13:44.978: E/AndroidRuntime(4244): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-27 15:13:44.978: E/AndroidRuntime(4244): ... 11 more
So for those who are interested, while I didn't discover the reason behind the problem, I found that I could prevent it. What I did when I started localizing was copy the entire values
folder and append the language code, making it values-fr
. There were two files in the folder, strings.xml
and styles.xml
, the latter of which were identical in both the default and -fr
versions.
Deleting styles.xml
from values-fr
, but leaving it intact in values
, resolved the problem (and also prevented the French interface from looking different from the English version).