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.


当前回答

对我有用的是:

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

其他回答

有一件事每次都对我有效:

转到——> build。你项目的gradle (Module: app)文件——>做一点改变(例如:把一个<空格>放在某个地方,然后把它移回来)——>然后android studio会要求你同步你的gradle文件——>在文件的右上角——>选择“立即同步”。

在同步完成后,它将解决大多数情况下的问题。

只需进入Android Top菜单列表。点击构建菜单,在构建下点击重建项目。

在我的情况下,我需要修复我的布局XML文件中的一些错误。没有指示文件包含错误的图标。然而,当我点击它们时,我注意到一些红线。修好后,找到了R。

这个符号看起来很好用。

android:id="@+id/viewID"

Android Studio的设计面板似乎不太好用。

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


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