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.


当前回答

清洁,重建和“缓存删除”不会帮助我,更新Android工作室修复了这个问题。

其他回答

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

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

我自己也费了好大劲才搞定的。

确保layout.xml文件中没有错误。 转到构建>清洁项目

这招对我管用,希望对你也管用。

你可以删除一个布局文件(不管哪个),之后你需要构建项目。在我的情况下,如果我试图在删除布局文件之前构建我的项目,它没有帮助我。但是这样也可以。

大多数情况下,此问题是由XML文件引起的,请查看XML文件,查找任何错误。

同样的问题。当我在我的可绘制文件夹中添加了一些图像并试图访问它们时开始。此外,添加的图像有大写字母的扩展名。这似乎使构建崩溃,因为即使我重命名它们,消息也是一样的,R是不可访问的。我所做的是,在.iml文件中,我寻找了excludeFolder并删除了它们(如下图所示):

  <excludeFolder url="file://$MODULE_DIR$/build/apk" />
  <excludeFolder url="file://$MODULE_DIR$/build/assets" />
  <excludeFolder url="file://$MODULE_DIR$/build/bundles" />
  <excludeFolder url="file://$MODULE_DIR$/build/classes" />
  <excludeFolder url="file://$MODULE_DIR$/build/dependency-cache" />
  <excludeFolder url="file://$MODULE_DIR$/build/incremental" />
  <excludeFolder url="file://$MODULE_DIR$/build/libs" />
  <excludeFolder url="file://$MODULE_DIR$/build/manifests" />
  <excludeFolder url="file://$MODULE_DIR$/build/res" />
  <excludeFolder url="file://$MODULE_DIR$/build/symbols" />
  <excludeFolder url="file://$MODULE_DIR$/build/tmp" />

在那之后,我重建了项目,R神奇地重新出现了。