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.


当前回答

在我的情况下,原因是有一些图像在res文件夹名称包含一个大写字母..

只要用小写字母重新命名就可以了

其他回答

更改最新的SDK版本,这对我来说是工作

1.单击文件,然后单击项目结构 2.然后选择app 然后点击确定,这对我有用。

还有一件事要尝试……

删除Build文件夹 通过同步gradle重新构建项目

我已经试过很多其他的建议了,比如

清洁项目, 重新启动Android Studio, 修复错误在我的xml, 同步项目与Gradle文件,以及 更新构建工具版本,

但这些都不起作用。

我最终手动删除了整个构建文件夹(在备份我的项目后),然后重新启动Android Studio。之后,我在我的应用程序构建中做了一个小编辑(添加和删除一个空格)。Gradle文件,然后点击同步。(我可能只需要从菜单中使用Gradle文件完成Tools > Android > Sync Project。)从那以后,它起作用了。在构建文件夹中已经损坏了一些东西,并开始与一个新的构建修复它。

我不知道有没有人读到这么远的答案,但我还是要加上这个答案。

我也有这个问题。简单的“gradlew clean”和“gradlew build”就成功了。


点击Build->Clean Project,将执行gradle Clean

你们最近更新了SDK工具吗?启动android SDK管理器,确保你有最新的SDK工具,现在是独立于平台工具。当我第一次更新我的SDK管理器时,我也遇到了同样的问题,SDK构建工具包在安装/更新时没有出现,直到我关闭并重新打开SDK管理器。

R.java文件包含xml和java页面之间的链接。只有在某些资源文件出现问题时,才会出现“R无法解决”。因此,最好和有效的方法是删除最后完成的xml或绘制在res文件。然后再根据android编码规范从那里开始。这是唯一的办法。有效而正确的方法。

也可以使用Git来正确地跟踪代码。