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.


当前回答

确保项目中有AndroidManifest.xml。

我导入的项目还没有manifest,所以Android Studio不会将其识别为Android项目。然后我在GIT中拉了这个项目,它下载了清单,但仍然不能工作。 然后我重新启动Android Studio,它说“框架检测到Android”。

确保你的项目配置为Android项目。

其他回答

只需清理你的项目和同步项目与Gradle文件。

问题会得到解决。

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

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

从顶部的菜单中选择:

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

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

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

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

我不得不在android studio中导入我的R包。例如:import com.example。<包名称>。R

我也遇到过同样的问题,当我创建一个新项目时,这种情况就会发生。

我所做的是:

检查SDK更新 然后是安卓工作室更新, 然后重新打开项目 打开andoridmanifest.xml 在android:标签中删除“_>”之间的空格并保存。

这对我很有用。

大多数情况下,此问题是由XML文件引起的,请查看XML文件,查找任何错误。