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.


当前回答

Build ->清理项目或重建项目 或 File -> Invalidate cache / Restart…->失效并重启

其他回答

在我的例子中,当我试图从另一个包调用R时,出现了这个错误。导入R包解决了我的问题。

import com.mtdaps.yourapp.R;

对我有用的是:

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

对我来说,问题是我升级到一个似乎有一些bug的Gradle版本。3.4.0-alpha02是我更新到的,发现这是“无法解析符号R”错误的罪魁祸首。我恢复到3.3.0-alpha10(即类路径'com.android.tools.build:gradle:3.3.0-alpha10'),它解决了这个问题。

这显然不是一个持久的解决方案,因为我迟早需要升级,但截至2018年11月3日,3.4.0-alpha02是最新版本,它阻碍了我的开发,因为它不允许我通过单击R引用在IDE中跳跃。

我在清理建筑。Gradle和这个错误出现。

一大堆“清理和重建”的答案。这些对我都没用。

最后,我发现我无意中复制粘贴了build.gradle中错误的“命名空间”名称。修复了它,它运行。

我确实做了一个项目,而且成功了 但在此之前,请尝试检查清单中的包名称,重新构建并清理项目。如果问题没有解决,那就放弃你的项目