NullPointerException 尝试访问 String 资源
我有以下 /res/values/uris.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="bladder">blahblah</string>
</resources>
我正在代码中访问它:
private String bladderUrl= getString(R.string.bladder);
但它返回 null。我不知道为什么?
I have the following /res/values/uris.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="bladder">blahblah</string>
</resources>
I am accessing it in code:
private String bladderUrl= getString(R.string.bladder);
But it is returning null. I'm not sure why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜你像这样放置了膀胱网址:
但是你需要这样做:
My guess you placed bladderUrl smth like this:
However you need to do smth like this:
首先要弄清楚这一点。您不必必须将所有字符串都放在
strings.xml
中。时期。伊曼纽尔的回答部分正确。当上下文初始化时,您必须在
onCreate()
方法中获取该字符串。他的回答只是部分正确,因为他还提到你必须将字符串放在 strings.xml 中,这是不正确的。First of all to clear that up. You don't have to have all your strings in
strings.xml
. Period.Emmanuels answer is partially right. You have to get the string inside your
onCreate()
method when the context is initialized. His answer is only partially correct since he also mentioned that you have to have the strings inside the strings.xml which is not true.首先,确保这些值位于文件 strings.xml 中。
此外,您无法从构造函数访问字符串。应用程序上下文尚未初始化。您应该在 onCreate() 中执行此操作。
以马内利
First, make sure these values are in the file strings.xml.
Also, you can't access the strings from the constructor. The application context is not yet initialized. You should do it in onCreate().
Emmanuel