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.


当前回答

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

import com.mtdaps.yourapp.R;

其他回答

更改最新的SDK版本,这对我来说是工作

1.单击文件,然后单击项目结构 2.然后选择app 然后点击确定,这对我有用。

我已经做了其他答案中提到的所有步骤,但它仍然没有得到解决。我正在使用android工作室花栗鼠,最后我能够通过删除来解决这个问题

maven { url "https://jitpack.io" }

从设置。gradle我在回答这个问题,我认为这样做可能会帮助到一些人

当我导入google服务时,我就遇到了这个问题。json文件来实现分析。我已经在xml文件夹中有global_tracker.xml文件。在构建过程中,合并来自google-services的内容。Json文件,错误开始发生。

删除google -services后,该错误得到解决。json文件。

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

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

FAILURE: Build failed with an exception.

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

还有一件事要尝试……

删除Build文件夹 通过同步gradle重新构建项目

我已经试过很多其他的建议了,比如

清洁项目, 重新启动Android Studio, 修复错误在我的xml, 同步项目与Gradle文件,以及 更新构建工具版本,

但这些都不起作用。

我最终手动删除了整个构建文件夹(在备份我的项目后),然后重新启动Android Studio。之后,我在我的应用程序构建中做了一个小编辑(添加和删除一个空格)。Gradle文件,然后点击同步。(我可能只需要从菜单中使用Gradle文件完成Tools > Android > Sync Project。)从那以后,它起作用了。在构建文件夹中已经损坏了一些东西,并开始与一个新的构建修复它。

我不知道有没有人读到这么远的答案,但我还是要加上这个答案。