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.


当前回答

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

其他回答

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

你们最近更新了SDK工具吗?启动android SDK管理器,确保你有最新的SDK工具,现在是独立于平台工具。当我第一次更新我的SDK管理器时,我也遇到了同样的问题,SDK构建工具包在安装/更新时没有出现,直到我关闭并重新打开SDK管理器。

同样的问题。当我在我的可绘制文件夹中添加了一些图像并试图访问它们时开始。此外,添加的图像有大写字母的扩展名。这似乎使构建崩溃,因为即使我重命名它们,消息也是一样的,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神奇地重新出现了。

看看Gradle控制台。在我的例子中,我包含了一个文件扩展名错误的资源。

AAPT: libpng error: Not a PNG file
:app:mergeDebugResources FAILED

FAILURE: Build failed with an exception.

在重命名文件和同步项目与Gradle文件之后,一切都很好。

我正在做一个多模块的项目。这发生在我把所有ui代码移到一个新模块时。

下面是我解决这个问题的方法: 我发现当我创建新的ui模块时,在/res/values中,它创建了一个新的strings.xml,其中没有任何有用的东西。 我删除了strings.xml文件,重新构建项目,它工作了。