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?

0

3 Answers 3

48

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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 That's probably because your Context is null at that point.
Try adding c = this; inside your onCreate() method
That too. But the this thing will fix your NPE.
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
|
8

Please refer to this StackOverFlow Question

String mystring = getResources().getString(R.string.mystring);

Comments

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.

6 Comments

If you read further down on your link, it clearly has setText(CharSequence text). A CharSequence is a String. See here.
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.
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.
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.
You mean it takes a String not an int...? And what method prototype?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.