0

I have a Java class that mainly contains strings. It does not have a layout as it is neither a Fragment nor an Activity. It is more used as an auxilliary class. I would like to assign the String values in the class by using the Resource strings as I would like to automatically translate the Strings. Howvever, I can't access the string resources from Java. I use the following code:

 static String more = getString(R.string.more);

And in the XML file I have the ressource:

<string name="more">More</string>

I get the error message

Cannot resolve method 'getString' 

I also tried static String more = getContext().getString(R.string.more); but I got the error message:

Cannot resolve method 'getContext'

Would anyone mind helping me on that? I'd appreciate every comment.

Update: I tried to use the answer from "MikaelM"

Resources.getSystem().getString(R.string.more)

However, I get an "exception in initializer error" and when I use the initial String again, I do not get this error. So I still can't get the String from the ressource. DO you have an idea what I can do? I'd appreciate every further comment. So the error is caused by "Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f120038" The strange thing is that the resource in fact exists (I checked this several times).

7
  • 1
    Does this answer your question? android how to use string resource in a java class Commented Sep 16, 2020 at 14:01
  • if you are in activity then use getString(R.string.more); directly instead of getContext().getString(R.string.more); Commented Sep 16, 2020 at 14:07
  • No, it does not answer my question because I do not have a context Commented Sep 16, 2020 at 14:07
  • Thanks Manohar for your comment. I am not inside an activity. As I wrote in the question, this is just a Java class without any layout. Commented Sep 16, 2020 at 14:08
  • Maybe remove static (because that is the only difference with MikaelM's answer). Commented Sep 17, 2020 at 9:35

2 Answers 2

1
getString(R.string...

is not the same as

Resources.getSystem().getString(android.R.string...

Only the second variant you can use in the static context. Alas, you can get this way only system resources.

If you need to get your own resources, no one-line solution exists. Use https://stackoverflow.com/a/4391811/715269 solution of @Cristian. And notice: it is a very good solution, even more, it is the solution meant to be used by the creators of Android.

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

4 Comments

Thanks Gangnus for your answer. What is the common approach for accessing strings for translations within Java classes in Android. Maybe a SQLite database that stores the strings. For Layouts you should use the ressources. I find it kind of weird that for normal classes you can't use them. What is your experience regarding this issue? Would be happy to hear you view
I also don't like that I can only take data as a source, but the prog cannot change them for the next launch. But that is due to the history of the question. The ways for reusing the changing start data are numerous: DBs, files, clouds...
@VanessaF For translated strings you just use the ressource from the depending language in the depending directory. developer.android.com/guide/topics/resources/localization You don't have to do anything more than have the ressource files. Android switches the language to the one you use on your system or the default if you don't have a strings.xml for it.
Thanks Jan for your answer. Basically I now use a database with all the strings for two different languages (I use one database for every language). And then I just make a database query to get the different strings in the correct language. I stopped using the resource files because I could not access the strings in a static context and I use most of the time the static context (I know that most developers don't recommend using static context; but for me it makes coding way easier and I see much more advantages than disadvantages)
1

You should be able to get your string with:

Resources.getSystem().getString(R.string.more)

See more here, getString Outside of a Context or Activity

11 Comments

Your answer produces a "exception in initializer error" in the class
The satement is mainly called from a static method. Maybe this has something to do why it is not working. But I am not sure and I do not know how to tackle this problem. So I'd appreciate every further comment.
The error is caused by "android.content.res.Resources$NotFoundException: String resource ID #0x7f120038""
Thanks MikaelM for your help so far. Any idea how I could tackle the problem mentiones above.
The answer might not be clear enough. It supports the system resources only and if you want local resources you need the context. This post might help you with your "context problem", [How can I get a resource content from a static context? ](stackoverflow.com/questions/4391720/…)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.