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).

VanessaF
  • 515
  • 11
  • 36
  • 1
    Does this answer your question? [android how to use string resource in a java class](https://stackoverflow.com/questions/8102741/android-how-to-use-string-resource-in-a-java-class) – forpas Sep 16 '20 at 14:01
  • if you are in activity then use `getString(R.string.more);` directly instead of `getContext().getString(R.string.more);` – Manohar Sep 16 '20 at 14:07
  • No, it does not answer my question because I do not have a context – VanessaF Sep 16 '20 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. – VanessaF Sep 16 '20 at 14:08
  • Maybe remove `static` (because that is the only difference with MikaelM's answer). – Joop Eggen Sep 17 '20 at 09:35
  • Thanks Joop for your answer. But I can't remove static because the string should be a static ressource. So it is not possible to remove it. The variable has to be a static one as I would like to refernce it without creating an object – VanessaF Sep 17 '20 at 09:36
  • Which class did you derive from that you thought you could use getString() or getContext()? Where should these methods come from? Methods don't appear from out of nowhere. – The incredible Jan Jul 29 '21 at 12:13

2 Answers2

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

MikaelM
  • 11
  • 2
  • Your answer produces a "exception in initializer error" in the class – VanessaF Sep 17 '20 at 09:03
  • 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. – VanessaF Sep 17 '20 at 09:14
  • The error is caused by "android.content.res.Resources$NotFoundException: String resource ID #0x7f120038"" – VanessaF Sep 17 '20 at 09:53
  • Thanks MikaelM for your help so far. Any idea how I could tackle the problem mentiones above. – VanessaF Sep 17 '20 at 12:48
  • 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? ](https://stackoverflow.com/questions/4391720/how-can-i-get-a-resource-content-from-a-static-context/4391811#4391811) – MikaelM Sep 17 '20 at 15:20
  • Thanks MikaelM for your comment and effort. What do you mean by 'local' and 'system' ressources. Basically I think that this is a system ressource. I just define it in the string resource XML file. – VanessaF Sep 17 '20 at 15:57
  • From what you are describing it sounds like your string is local resource. As far as I understand it the Resources.getSystem().getString() solution works with common system resources (android.R.string), like "android.R.string.untitled". Check the imports in your class: import android.content.res.Resources; import se.yourproject.R; – MikaelM Sep 17 '20 at 16:22
  • Thanks Mikael for your answer. Basically the solution for the local ressource (the link you posted) seems extremely complicated and I doubt that it will be suitable for my case as they use a context and I do not have a context because I am in a Java class that has nothing to do with an activity of a fragment. So I think it won't work – VanessaF Sep 17 '20 at 17:32
  • 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. – VanessaF Sep 17 '20 at 17:34
  • Im not sure if I follow anymore. Where is the class used then or what is using the class? Will it not be used when the application is running? Unless you add some more example code I don't think I have any more ideas. Not saying I will have one if you do :) – MikaelM Sep 18 '20 at 12:01
  • Thanks Mikael for your answer and effort. Basically the class is used by a Fragment that is running (but the class itself is not a fragment or activity but just a helper class). – VanessaF Sep 18 '20 at 12:10
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.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • 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 – VanessaF Sep 18 '20 at 11:26
  • 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... – Gangnus Sep 18 '20 at 19:45
  • @VanessaF For translated strings you just use the ressource from the depending language in the depending directory. https://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. – The incredible Jan Jul 29 '21 at 12:25
  • 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) – VanessaF Jul 29 '21 at 20:25