无法解析资源android eclipse
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16732198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
could not resolve resource android eclipse
提问by dav
I am new in andorid development. I have created my android first project according to http://developer.android.com/training/basics/firstapp/creating-project.html
我是安卓开发的新手。我已经根据http://developer.android.com/training/basics/firstapp/creating-project.html创建了我的 android 第一个项目
And here is the code that I have as the project was created(no changes are done in code by me)
这是我在创建项目时拥有的代码(我没有对代码进行任何更改)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
Here is the screenshot of the Graphical layout http://imageshack.us/a/img35/5284/android1s.png
这是图形布局的截图 http://imageshack.us/a/img35/5284/android1s.png
So, it looks fine for now, but if I want to change the text Hello world
to , say, hello
, so the code's part looks like android:text="@string/hello"
, I get the error
所以,现在看起来不错,但是如果我想将文本更改Hello world
为 ,例如,hello
代码部分看起来像android:text="@string/hello"
,我会收到错误
NOTE: This project contains resource errors, so aapt did not succeed, which can cause rendering failures. Fix resource problems first.
NOTE: This project contains resource errors, so aapt did not succeed, which can cause rendering failures. Fix resource problems first.
and
和
Couldn't resolve resource @string/hello
, and the text in graphical layout becomes
@string/hello
, as it is in this image
Couldn't resolve resource @string/hello
, 图形布局中的文本变为
@string/hello
, 就像在此图像中一样
http://imageshack.us/a/img826/7735/android2p.png
http://imageshack.us/a/img826/7735/android2p.png
But I noticed one thing:
If after this error I go to res/values/strings.xml
and add String
with name and value hello
, like
但我注意到一件事:如果在这个错误之后我去res/values/strings.xml
添加String
name 和 value hello
,比如
http://img842.imageshack.us/img842/8108/android3s.png
http://img842.imageshack.us/img842/8108/android3s.png
The error disappears and I can see the word hello
in the graphical layout.
错误消失了,我可以hello
在图形布局中看到这个词。
I tried these so far
到目前为止我试过这些
- Using
ctrl+shift+s
like it is described here https://stackoverflow.com/a/14462434/932473 - Closing and reopening Eclipse.
- cleaning, rebulding
- removing the entire project and creating again.
ctrl+shift+s
像这里描述的那样使用https://stackoverflow.com/a/14462434/932473- 关闭并重新打开 Eclipse。
- 清洁、重组
- 删除整个项目并重新创建。
The values
folder does exist in res
.
该values
文件夹确实存在于res
.
None of these questions solved my problem
这些问题都没有解决我的问题
Eclipse could not resolve string resource error
Can't Resolve Android Resource Strings
UPDATEDIf I write like this android:text="hello"
I get this warning in eclipse
Hardcoded string "hello", should use @string resource
,
更新如果我这样写,android:text="hello"
我会在 eclipse 中收到这个警告
Hardcoded string "hello", should use @string resource
,
I do not get it every time I want to use some string like android:text="@string/hello2"
, I have to first manually specify hello2
it in strings.xml
?
每次我想使用一些字符串时我都没有得到它android:text="@string/hello2"
,我必须先hello2
在strings.xml
?
回答by Raghunandan
In res/values/strings.xml
在 res/values/strings.xml
<string name="hello">Hello</string>
Then refer the string in your textview as
然后将文本视图中的字符串引用为
android:text="@string/hello"
I guess you do not have something similar to above. Hence you get resource not found exception coz the resource is not defined in strings.xml
我想你没有与上面类似的东西。因此你得到资源未找到异常因为资源没有在 strings.xml 中定义
or
You can do as below
你可以做如下
<string name="hello_world">hello</string> // change the string in strings.xml
android:text="@string/hello_world"
For the question in the comment
对于评论中的问题
回答by Doomsknight
You can just use
你可以使用
android:text="hello"
the @string bit means its a resource called hello
in your xml.
@string 位表示它是hello
在您的 xml 中调用的资源。
So you can also add the resource to your xml if you wish. Its known to be better practice, for internationalization.
因此,如果您愿意,您还可以将资源添加到您的 xml 中。众所周知,它是更好的实践,用于国际化。