Localizing strings in strings.xml gives NullPointerException
Asked Answered
T

2

0

My work computer that Eclipse is installed on does not have internet connectivity due to work related issues so all code and LogCat text has been hand typed instead of copy and pasted since I am on a separate laptop that Eclipse is installed right now. So bear with me for any typos.

Now to the issue. In the new version of my app, I am making it Spanish supported. I localized all my strings in strings.xml. Below is my Java code that I am not usuing to implement.

public class SplashScreen extends SwarmActivity {

  Context c;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashscreen);

    loading = (TextView)findViewById(R.id.loading);
    //loading.setText(c.getResources().setString(R.string.loading));  //This way gives NPE
    //loading.setText(R.string.loading);  //This way works
    //loading.setText("Test");  //This way works
  }
}

If I understand localization correctly, I have to getResources() first so the app knows what language of the string to display. But the getResources() is what is messing me up.

What do I need to do to get the string displaying correctly?

Tuyettv answered 3/5, 2013 at 15:29 Comment(0)
B
2

To answer your problem, your forgot to initialize your Context object. So c is null. Replace loading.setText(c.getResources().setString(R.string.loading)); by

loading.setText(getResources().setString(R.string.loading));

But actually there is no need to do that.

Android loads the appropriate resources according to the locale settings of the device at run time.

You just have to respect this hierarchy in your project :

res/
       values/
           strings.xml
       values-es / (here for spanish values)
           strings.xml
       values-fr /
           strings.xml (here for french values)
Brote answered 3/5, 2013 at 15:31 Comment(3)
So are you saying all I have to do is put loading.setText(R.string.loading); and Android will know at runtime based on locale settings of the device being used whether to output the English or Spanish string?Tuyettv
Yes, if you respect the hierarchy that I described, read this for more informations : developer.android.com/training/basics/supporting-devices/…Brote
Seems to all be compiling correctly. If any hiccups arise, I will update.Tuyettv
B
0

You have this code

Context c;

public void onCreate(Bundle savedInstanceState) {
    ...
    loading.setText(c.getResources().setString(R.string.loading));  //This way gives NPE

The member c is never set before it is used. This is the reason for the NullPointerException. You must first initialize c with View.getContext() for example.

Localization is handled automatically according to the device's capabilities and settings.

In your layout definition, you can define the text string with a reference to a string id and Android will automatically load the appropriate resource

In res/layout/splashscreen.xml:

...
<TextView android:id="@+id/loading"
    android:text="@string/loading"
    .../>
...

So there is no need to explicitly set the text string in your code, because Android will do so already. The only thing you have to do, is to define the appropriate text strings in the res/values*/strings.xml files.

Brownell answered 3/5, 2013 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.