2

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
GarrickW
  • 2,181
  • 5
  • 31
  • 38
  • 3
    Posting the stack trace would help. – CommonsWare Apr 27 '13 at 19:27
  • Added the log info above. – GarrickW Apr 27 '13 at 19:34
  • What is line 34 of `ScreenDetailActivity`? – CommonsWare Apr 27 '13 at 19:35
  • getActionBar().setDisplayHomeAsUpEnabled(true); – GarrickW Apr 27 '13 at 19:38
  • 1
    Then `getActionBar()` is returning `null`. Note that if you are using ActionBarSherlock, I think you should be calling `getSupportActionBar()`. – CommonsWare Apr 27 '13 at 19:39
  • Hm... could this be related to the fact that, for some reason, the entire Android interface looks a bit different when I switch to French? The colors change from black-blue to black-green and the fields have white backgrounds and look boxier. In any case, I am not using any extensions, just stock Android functions. – GarrickW Apr 27 '13 at 19:45
  • 1
    That means you're not getting a reference to an ActionBar. Check to make sure you've not overridden your Activity style in a `-fr` resource folder of some sort to a theme that does NOT include an ActionBar. – Kevin Coppock Apr 27 '13 at 20:02
  • The `styles.xml` file in both `values` and `values-fr` are identical. The only other localized files are the `string.xml` files, which are also identical except for the content of individual strings. – GarrickW Apr 27 '13 at 20:09
  • 1
    @GarrickW If they're identical, why have them in both folders? – Kevin Coppock Apr 27 '13 at 20:13
  • So I deleted the styles.xml file in values-fr and, for whatever reason, suddenly everything works as intended. Thank you! – GarrickW Apr 27 '13 at 21:31

2 Answers2

2

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).

GarrickW
  • 2,181
  • 5
  • 31
  • 38
0

Ensure 2 things: 1) You have suppressed the Lint error by having @SuppressLint("NewApi") on the top of onCreate method. (Which I guess probably you have already done!) 2) You must include minSdk specified in the Android manifest file. Keep these lines: (just right after the manifest tag)

 <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" /> 

Hope that helps!

Sudipto
  • 1
  • 1