android - 无法解析资源android eclipse

标签 android eclipse

我是 andorid 开发的新手。 我根据 http://developer.android.com/training/basics/firstapp/creating-project.html 创建了我的第一个 android 项目

这是我创建项目时的代码(我没有对代码进行任何更改)

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

这是图形布局的屏幕截图 http://imageshack.us/a/img35/5284/android1s.png

所以,现在看起来不错,但是如果我想将文本 Hello world 更改为,比如说,hello,那么代码部分看起来像 android:text="@string/hello",我得到错误

注意:此项目包含资源错误,所以 aapt 没有成功,这可能会导致渲染失败。先修复资源问题。

Couldn't resolve resource @string/hello,图形布局中的文本变为 @string/hello,如图所示

http://imageshack.us/a/img826/7735/android2p.png

但是我注意到一件事: 如果出现此错误后我转到 res/values/strings.xml 并添加 String 名称和值 hello,例如

http://img842.imageshack.us/img842/8108/android3s.png

错误消失,我可以在图形布局中看到单词 hello

到目前为止我都试过了

  1. 使用 ctrl+shift+s 就像这里描述的那样 https://stackoverflow.com/a/14462434/932473
  2. 关闭并重新打开 Eclipse。
  3. 清洁、重建
  4. 删除整个项目并重新创建。

values 文件夹确实存在于 res 中。

这些问题都没有解决我的问题

Eclipse could not resolve string resource error

Can't Resolve Android Resource Strings

已更新 如果我这样写 android:text="hello" 我在 eclipse 中收到这个警告 硬编码字符串“hello”,应该使用@string资源,

每次我想使用像android:text="@string/hello2"这样的字符串时,我都没有得到它,我必须先手动指定hello2它在 strings.xml 中 ?

最佳答案

在 res/values/strings.xml 中

      <string name="hello">Hello</string>

然后将 TextView 中的字符串引用为

      android:text="@string/hello"

我猜你没有类似上面的东西。因此,您会收到资源未找到异常,因为资源未在 strings.xml 中定义

                 or

你可以按照下面的方式做

       <string name="hello_world">hello</string> // change the string in strings.xml      
       android:text="@string/hello_world"  

对于评论中的问题

enter image description here

enter image description here

关于android - 无法解析资源android eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16732198/

相关文章:

java - 如何在java中使用NSApplication和NSWindow访问在mac中打开的窗口及其内容?

eclipse - 无法让 Android ApiDemos 在 Eclipse 中工作

java - 使用 JRE 或 JDK 编译的 Eclipse

android - Caffe:检查失败:网络应该只有一个输出

android - imageSwitcher 和内存错误

android - 如何处理构建 API 版本 > minSdkVersion?

java - 多文件上传只上传最后一个文件而不上传其余文件

android - 如何删除日历条目?

eclipse - 将现有的SVN项目导入Eclipse

eclipse - m2eclipse 不显示工作区项目依赖项,但显示传递依赖项