【问题标题】:Android development R cannot be resolved to a variable (no resource found error)Android开发R无法解析为变量(no resource found错误)
【发布时间】:2014-05-09 03:12:05
【问题描述】:

昨天我正在开发我的 Android 应用程序,它运行良好。但是今天当我打开它时,我收到了错误R cannot be resolved to a variable,我不知道为什么会这样。在控制台中我收到此错误[2014-03-29 11:04:59 - RockPaperScissors] C:\Users\hosfo_000\workspace2\RockPaperScissors\res\menu\main.xml:6: error: Error: No resource found that matches the given name (at 'title' with value '@string/action_settings'). 有谁知道可能是什么原因造成的?我已经尝试清理项目,但没有奏效。

编辑

我的 main.XML 的第 6 行是 <item 所以我添加了该项目的其余部分。我的整个 main.xml 包含这个项目。

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never"/>

【问题讨论】:

  • 您使用的是正确的 R 吗?检查以确保您没有使用 android.R

标签: java android resources r.java-file


【解决方案1】:

您确定字符串 action_settings 存在于 res/values/strings 中吗?

将此添加到 res/values/strings 文件中

<resources>
<string name="action_settings">Action Settings</string>
<string name="app_name">The name of your app</string>
</resources>

【讨论】:

  • 不,那个文件是空的。那是问题吗?我将如何解决它?
  • @RyanHosford 是的,这就是问题所在,请查看我的编辑,如果答案有用,请接受或/并投票赞成 :-)
  • 我有。还有其他想法吗?
  • @RyanHosford OK,保存您的项目,然后转到 Project->Clean 并选择您的项目并按 OK
  • @RyanHosford 请在项目的 gen 文件夹中查看,在包名称下您应该看到 R.java。你看到了吗?
【解决方案2】:

如果文件为空,则需要创建它....

把它放在上面命名的资源文件中。

<string name="action_settings">Your Text Here</string>

90% 的 R 错误是因为 xml 错误

C:\Users\hosfo_000\workspace2\RockPaperScissors\res\menu\main.xml:6: error: Error:

【讨论】:

  • 我认为没有 XML 错误,因为昨天一切正常
  • 第六行是什么?对上述字符串的引用...您需要确保字符串中的拼写正确,然后清理您的项目
  • 我把你的代码加到main.xml中了,错误依旧存在
  • 你想打开你的资源文件夹,然后打开strings xml,把它放在那里。如果您的字符串文件夹中没有其他内容,它应该看起来像这样
  • 动作设置
猜你喜欢
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
  • 2015-01-31
  • 2013-05-14
相关资源
最近更新 更多