20

This is sort of a continuation of this thread:

Localizing strings in strings.xml gives NullPointerException

This issue arose after going through and implementing the solution provided in that thread. The solution provided worked for instances like this:

loading = (TextView)findViewById(R.id.loading);
loading.setText(R.string.loading);

This does not work for situations like below:

exodus.add(new Question(R.string.a, R.string.b, R.string.c, R.string.d, 0, R.string.e, -1, R.string.f));

The error is The constructor Question(int, int, int, int, int, int, int, int) is undefined. The contructor for Question is supposed to be Question(String, String, String, String, int, String, int, String) but these:

R.string.X

are returning as ints instead of Strings.

What can I do to fix this problem? And also, if you know, why is this method working for setting text of TextViews but not parameters in objects?

Community
  • 1
  • 1
Matt
  • 3,882
  • 14
  • 46
  • 89

3 Answers3

46

R.string.* is a reference to an int in R.java that points to your actual String.

Use context.getResources().getString(R.string.*); to get the actual String value.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Please refer to the link in my opening post. That is what I was doing initially but that was not working. It was giving me a NPE. – Matt May 03 '13 at 17:18
  • @Matt That's probably because your Context is null at that point. – Raghav Sood May 03 '13 at 17:19
  • 1
    Try adding `c = this;` inside your onCreate() method – Raghav Sood May 03 '13 at 17:20
  • @Matt or you could use just `getResources().getString(R.id.*);` – TronicZomB May 03 '13 at 17:22
  • 1
    That too. But the `this` thing will fix your NPE. – Raghav Sood May 03 '13 at 17:23
  • @RaghavSood - The `c = this;` seems to be what I was looking for. One more question: for the classes that don't extend Activity, where do I initialize the `Context c`? It seems whereever I put it I get a `type mismatch` error. For example, the `exodus.add(new Question(c.getResources().getString(R.id.*, ....);` is in a class that does not extend an activity. – Matt May 03 '13 at 17:43
  • 1
    Matt, Since it's not an activity, you can use the application's context by calling getApplicationContext().getResources().... Or, create a class that extends Application (if you already don't have it), say that class is called MainApp (extend Application), create a private static variable private static MainApp appContext;, and simply create a public static getAppContext(){return appContext;} Now you can just call MainApp.getAppContext() if you need context outside of an activity or service – vkinra May 03 '13 at 18:05
  • Thank you @vkinra! All working! – Matt May 03 '13 at 18:26
  • @RaghavSood - Feel free to post the correct answer on that other thread and I will mark your answer as correct. – Matt May 03 '13 at 18:26
6

Please refer to this StackOverFlow Question

String mystring = getResources().getString(R.string.mystring);
Community
  • 1
  • 1
MDMalik
  • 3,951
  • 2
  • 25
  • 39
4

For your last question:

You are using the setText method prototype which takes a resid argument, not a String, that's why it's working. R.string.loading returns an int representing that resid. Had you been calling the setText method with the getString(R.string.loading) argument, then it would have used the setText prototype.

Voicu
  • 16,921
  • 10
  • 60
  • 69
  • If you read further down on your link, it clearly has `setText(CharSequence text)`. A `CharSequence` is a `String`. See [here](http://stackoverflow.com/questions/1049228/charsequence-vs-string-in-java). – TronicZomB May 03 '13 at 17:27
  • 1
    `R.string.loading` represents an `int` (check R class to see its value), hence the method I mentioned. If you do `getString(R.string.loading)`, then you have a string to use in the method you mentioned. – Voicu May 03 '13 at 17:47
  • `R.string.loading` is an `int`, I'm not disputing that and I know that already. But what you are stating in the answer is that you cannot use a `String` in `setText`, a `String` is different from `R.string`, and you can in fact use `String` with `setText`. So what you are saying in your answer is still wrong. – TronicZomB May 03 '13 at 18:51
  • I'm not saying that he cannot use a `String`, but that the method prototype he's using takes an `int`, not a `String`, hence the reason why it's working for him. – Voicu May 03 '13 at 18:57
  • You mean it takes a `String` not an `int`...? And what method prototype? – TronicZomB May 03 '13 at 19:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29389/discussion-between-voicu-and-troniczomb) – Voicu May 03 '13 at 19:01