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.


当前回答

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


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

其他回答

我已经做了其他答案中提到的所有步骤,但它仍然没有得到解决。我正在使用android工作室花栗鼠,最后我能够通过删除来解决这个问题

maven { url "https://jitpack.io" }

从设置。gradle我在回答这个问题,我认为这样做可能会帮助到一些人

在xml文件中简单地定义一个应用程序名称空间就会导致这种情况。这是一个完全无害的行,只选择了一个符号(从许多符号中)来解析。简单地删除整行,在下面的图片中灰色,使我的代码引用未解析的符号。甚至当我添加回线,它决定没有问题。图。

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

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

我想我有终极解决方案了。我做了一项研究,发现Android Studio讨厌在安装路径或目录中设置“空白”。它不接受放置在包含空白的目录中的SDK。例如

C: \ Program Files \ Android \ sdk (x86)

此目录无效,因为它包含空白。在这里,我已经回答了一个问题,一个人无法在Android Studio中更改他的JDK路径,因为他的SDK目录中有一个空白。这很奇怪,但似乎Android Studio讨厌有空格的目录。所以解是-

将Android Studio和Android SDK安装在一个没有任何空白的目录或路径中。

例如-安装Android Studio

C:\AndroidStudio\

并安装Android SDK

C:\Sdk\

这两个目录都是有效的,因为没有任何空白。每个不包含空格的路径都是有效的。它还解决了其他问题,如“Failed to resolve: junit:junit:4.12”或“Gradle同步项目失败”等。

这是最近删除的xml造成的 在Android Studio中,从这些xml中清除你的项目,选择: Build ->清洁项目