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控制台。在我的例子中,我包含了一个文件扩展名错误的资源。

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

FAILURE: Build failed with an exception.

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

其他回答

这是最近删除的xml造成的 在Android Studio中,从这些xml中清除你的项目,选择: Build ->清洁项目

只需从根包中导入R符号

import rootpackage.R;

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

我知道这个问题可能会导致很多事情。在我的例子中,我在其中一个活动*.xml文件中有一些textView。我在编辑器中放入文本(稍后通过代码更改):“NAME: <这里将是名称>”。显然,“<”和“>”字符破坏了在我的项目中从每个地方导入R。我把它换了,它开始工作了。我只是忘记了不能在*.xml文件中使用这些字符,因为它们构建了它的结构。记住这一点是值得的。

请看下面的图片去文件->项目设置->设置所有设置像图像和同步它将把所有最新的设置在你的项目。 如果您的项目导入成功,那么您必须这样做。因为它会导入所有的更新设置和gradle。

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

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