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.


当前回答

伙计们,我觉得这里有很多是瞎拍。在某些情况下,只有在解决了问题之后,“清洁和同步项目”才会有所帮助。

步骤1: 因此,去看看每个文件的顶部,其中包被指定如下:package ="YourURL.YourProject";并确保指定了正确的包(Your Own Project)。 你会在java文件和AndroidManifest.xml中发现这一点,对于java文件来说,引用正确的包是至关重要的,因为这个包包含了你所指向的资源(“R”)。如果他们不匹配,错误不能解决符号R发生。

步骤2: 清洁,同步,等等。完成

为什么这是随机发生的,或者我做错了什么?

如果复制和粘贴代码,应该密切注意上面解释的“package=”。更重要的是,当你粘贴代码时,它会立即通过某种调试器(请原谅我的糟糕技术术语)向你显示“假定错误”,它会立即考虑“错误的包”,你会得到所有的错误。 因此,即使您在粘贴后立即更正了符号,调试器也已经刷新。这就是为什么“清洁,同步,随便什么”有时管用

其他回答

我在清理建筑。Gradle和这个错误出现。

一大堆“清理和重建”的答案。这些对我都没用。

最后,我发现我无意中复制粘贴了build.gradle中错误的“命名空间”名称。修复了它,它运行。

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

修复“无法解析符号r”的错误。 试试这些解决方案。

Build ->清洁项目。 Build -> Build Project。 Build ->制作项目。 工具栏左起第三个图标->同步项目与gradle文件。 File ->使缓存失效并重新启动。 确保您在应用级gradle文件中使用兼容的gradle版本。

例如:“classpath 'com.android.tools.build:gradle:X.X.X'”。

我希望这些解决方案能帮助到你。

感谢阅读。

在我的情况下,原因是有一些图像在res文件夹名称包含一个大写字母..

只要用小写字母重新命名就可以了

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