In every instance in all of my classes where I reference R.id.something, the R is in red and it says "cannot resolve symbol R". Also every time there is R.layout.something it is underlined in red and says "cannot resolve method setContentView(?)". The project always builds fine. It is annoying to see this all the time. I have read many other questions on here about something similar but most involved importing projects from Eclipse. I am using what I believe to be the most recent version of Android Studio and the project was created with Android Studio and worked without any "cannot resolve R" problems. I would like to know what causes this if anyone knows.


当前回答

我最近遇到了这个问题,清洁/构建等都没有解决它。我猜我在某个地方有一个问题,但不确定在哪里找到它(它是在清单,资源xml文件等?)

当出现问题时,您可以通过运行代码分析来找出问题所在。

从顶部的菜单中选择:

检查代码,使用'Default'配置文件扫描整个项目。

您的检查结果将包含一个名为“Android”的元素。在这里,您将获得导致资源和清单文件中出现问题的所有内容的列表。这些错误将打开生成的xml文件,并以红色显示任何错误。

我的确切问题是由于我使用基于flavor的清单文件,并且我意外地在flavor中的<application>标签内复制了一些权限。

更新:我确实有一个问题后,Android工作室仍然显示错误后修复,即使我现在可以构建和运行完美,而错误仍然存在。(我正在运行一个金丝雀构建,所以现在把它放下来)

其他回答

对我来说,重要的是说Android Studio在一开始就会给学习带来麻烦,但就像这里有解决方案清洁项目可以解决这个问题,总是检查包装的名称,不要试图混乱的逻辑,我喜欢软件,但有时我试着思考如果方式是正确的或不正确,我也喜欢Linux,但对我来说,Android Studio重写所有软件也许这不会发生。总之,祝你好运!!!!

对我有用的是:

创建一个新项目。 发现R在工作! 比较了所有的配置。 在gradle文件中发现了差异: 同步,它又工作了!

Build ->清理项目或重建项目 或 File -> Invalidate cache / Restart…->失效并重启

首先检查是否有任何错误在任何xml布局,如果然后解决它首先。

否则,从项目中删除junit依赖项并重新构建项目。

“我也遇到过同样的问题,解决方法是:

1) Sync Project with gradle files
2) Build -> Clean Project
3) Build -> Rebuild Project
4) File -> Invalidate caches

//imp step
5) Check your xml files properly.`