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.


当前回答

当我手动重命名应用程序的域文件夹时,我遇到了这个问题。为了解决这个问题,我不得不这样做

在AndroidManifest.xml中设置正确的<manifest>的包文件夹结构。 在AndroidManifest.xml中为android设置新的包位置:<activity>的名称。 清除缓存

文件菜单-> Invalidate cache / Restart…

一旦Android工作室重新启动并构建新的索引,这个问题就会消失。

其他回答

这让我很头疼。在我的案例中,在Android Studio中配置“谷歌播放服务”后出现了错误(安装+ build。gradle配置)。这反过来需要minsdk > 7,但错误是非常不清楚的(实际上一个抱怨这只出现为“信息”)。总之. .在清单文件和构建中将minsdk更改为8。Gradle文件和重建项目,错误消失了。

所以. .关于这一点,如果我可以概括的话,你可能在你的清单文件或其他配置文件中有一些问题,这阻碍了正确的构建。试着仔细查看错误报告,甚至是标题为“info”的消息,以寻找任何关于它可能是什么的提示……

只需从根包中导入R符号

import rootpackage.R;

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

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

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

FAILURE: Build failed with an exception.

在重命名文件和同步项目与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中跳跃。

在最新版本的Android Studio中,至少对我来说,如下作品:

“工具”->“Android”->“同步项目与Gradle文件”

在最新的Android Studio 3.1.3(2018年7月)中,“同步项目与Gradle文件”在主菜单栏中可用。