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 TextView
s but not parameters in objects?
android:text="@string/hello" /> String string = getString (R. string. hello); You can use either getString(int) or getText(int) to retrieve a string.
Format string XXX is not a valid format string so it should not be passed to String.
You can find strings. xml file inside res folder under values as shown below.
You should add them to a string resource file and then reference them from your layout. This allows you to update every occurrence of the word "Yellow" in all layouts at the same time by just editing your strings. xml file. It is also extremely useful for supporting multiple languages as a separate strings.
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.
Please refer to this StackOverFlow Question
String mystring = getResources().getString(R.string.mystring);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With