从类中调用 getString(R.strings....) ?
有没有办法使用单独的类中的 getString 方法?
我的字符串 xml 中存储了一个字符串,我想在对象中使用该字符串...但该方法甚至在对象中不可用...
有什么提示吗?
Is there a way to use the getString method from a seperate class?
I have a string stored in my strings xml, I'd like to use that string in an object... but the method isn't even available in the object...
any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
getString()
是Context 的方法
类 1。如果您在单独的类(不扩展 Context)中需要它,通常最好将其作为单独的参数提供给需要它的方法。示例:
有一件事很重要:永远不要将上下文存储在单独的类中。
提供一个论据。否则,如果存储上下文的对象比上下文最初所属的对象(例如活动)的寿命长,您将泄漏内存并破坏整个 android 生命周期。
¹
getString()
也可以在Resources
类 - 您可以通过Context.getResources()
获取getString()
is a method of theContext
class¹. If you need it inside a seperate class (that does not extendContext
), it's usually best to provide it as a seperate argument to the method that needs it.Example:
One thing is important: Never store the context inside the separate class.
Provide an argument. Otherwise you will leak memory and disrupt the whole android lifecycle if the object that stores the context lives longer than the object where the context originally belongs to (e.g. an activity).
¹
getString()
can also be used from theResources
class - which you can get viaContext.getResources()
这里的解决方案是确保您的对象具有对应用程序上下文的引用
,显然您必须在创建对象或调用方法时提供上下文
the solution here is to make sure your object has a reference to the application context
obviously you'd have to supply the context when creating an object or when caling the method
资源文件:values/strings.xml
java
resouce file: values/strings.xml
java
编辑:
下面的将不起作用。我在另一个网站上读到了这篇文章并认为它有效,但我只是在我的应用程序中尝试过它并不断收到错误。问题是,它会编译,但你会得到一个运行时异常。
这适用于任何 java 类:
edit:
The below will NOT work. I read this on another site and assumed it worked, but I just tried it in my app and kept getting an error. Problem is, it will compile but you will get a runtime exception.
This will work from any java class:
如果您无法将上下文作为参数传递,请创建另一个类,在其中放置所有静态数据。
示例:
并通过直接调用从其他类获取该字符串
StaticData.BASE_URL
干净整洁。
if you cannot pass a context as parameter, create another class, where you put all your static data.
example :
and get that string from your other class by calling directly
StaticData.BASE_URL
nice and clean.
最佳解决方案在这里:
首先,您应该将 ApplicationContext 注入到您的类中,然后调用
ContextCompat.getContextForLanguage(applicationContext)
,然后在此上下文上getString(resId)
。文档说这个方法
,因此它非常适合较低 (<= 32) API 版本,其中应用程序上下文未在应用程序语言环境更改时配置,并且调用
getString(resId
将会使在默认区域设置中返回的此字符串非常安全,因为它的生命周期基于进程生命周期,因此不会泄漏。
Best solution is here:
At first, you should inject ApplicationContext to your class and then call
ContextCompat.getContextForLanguage(applicationContext)
and thengetString(resId)
on this context.The documentation says that this method
So it's pretty suitable for lower (<= 32) API version where the app context is not configured on app locale changes and calling
getString(resId
will make this string returned in your default locale.Injecting applicationContext is pretty safe because it's lifetime bases on the process lifetime so it can't be leaked.
这有效,但仅适用于系统资源:
参考:https://stackoverflow.com/a/40917607/8994882
This works, but for SYSTEM resources only:
Reference: https://stackoverflow.com/a/40917607/8994882
在您的 java 文件中尝试以下操作:
String myString = getResources().getString(R.string.MY_STRING)
现在使用此字符串对象。
Try this in your java file:
String myString = getResources().getString(R.string.MY_STRING)
Now use this string object.