如何在 Android 的 Java 文件中在没有布局的情况下使用字符串资源。

huangapple go评论70阅读模式
英文:

How to use String ressource in Java file in Android without a Layout

问题

我有一个Java类,主要包含字符串。它没有布局,因为它既不是Fragment也不是Activity。它更多地被用作辅助类。我想通过使用资源字符串来为类中的字符串赋值,因为我想自动翻译这些字符串。然而,我无法从Java中访问字符串资源。我使用以下代码:

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

在XML文件中,我有资源定义:

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

我得到错误消息:

Cannot resolve method 'getString'

我还尝试了 static String more = getContext().getString(R.string.more);,但是我得到了错误消息:

Cannot resolve method 'getContext'

有人介意帮我解决这个问题吗?我会感谢每一条评论。

更新:我尝试了“MikaelM”的答案:

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

然而,我得到一个“初始化器错误”的异常,当我再次使用初始字符串时,我不会得到这个错误。所以我仍然无法从资源中获取字符串。你有任何想法我该怎么做吗?我会欣赏每一条进一步的评论。

因此,错误的原因是“Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f120038”。奇怪的是,实际上这个资源是存在的(我多次检查过)。

英文:

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:

&lt;string name=&quot;more&quot;&gt;More&lt;/string&gt;

I get the error message

Cannot resolve method &#39;getString&#39; 

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

Cannot resolve method &#39;getContext&#39;

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&quot;
The strange thing is that the resource in fact exists (I checked this several times).

答案1

得分: 1

你应该能够通过以下方式获取你的字符串:

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

在这里查看更多信息,https://stackoverflow.com/questions/4253328/getstring-outside-of-a-context-or-activity

英文:

You should be able to get your string with:

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

See more here, https://stackoverflow.com/questions/4253328/getstring-outside-of-a-context-or-activity

答案2

得分: 1

getString(R.string... 在静态上下文中不同于 Resources.getSystem().getString(android.R.string...。只有第二种变体可以在静态上下文中使用。不幸的是,你只能通过这种方式获取系统资源。

如果你需要获取你自己的资源,就没有一行代码的解决方案。使用 @Cristian 在 https://stackoverflow.com/a/4391811/715269 中提供的解决方案。请注意:这是一个非常好的解决方案,甚至可以说是 Android 的创建者们打算使用的解决方案。

英文:
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.

huangapple
  • 本文由 发表于 2020年9月16日 21:56:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63921633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定