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.


当前回答

在我做“建立->清洁项目”和“同步项目与gradle”。它们都不是解。我从3.3.0 => 3.2.1(恢复为项目初始状态)下构建gradle版本,它解决了我的问题。

其他回答

我也有同样的问题。我尝试了上面提到的大多数其他人,即Gradle同步,验证缓存和重新启动,清洁和重建项目…重新启动android studio,仍然有同样的问题…

这是我的问题,我认为其他人可能也有同样的问题。我在新创建的布局文件的名称上有空格…而不是file_example.xml我是文件_example.xml **(注意文件和_之间的空格)**当你试图找到这个布局文件的引用时,你会遇到一个错误。

我建议您查看您刚刚创建的最新布局文件,稍微重构名称,然后将您的R.layout更改为activity/fragment中的新文件—> 然后清理..重建项目,项目将编译没有任何错误。至少对我来说,这是解决问题的办法。

只需从根包中导入R符号

import rootpackage.R;

没有重建,同步或其他事情…

我受够了

我可绘制资源中的一个大写字母。 导入安卓。R由Android Studio(或Eclipse)添加 xml文件错误

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


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

     The above answers are also the ways to fix the error but if does not work for anyone  then i have an option...

只需要关闭项目,然后重新启动它,这样它就会加载引用… 跟着截图走。