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文件中简单地定义一个应用程序名称空间就会导致这种情况。这是一个完全无害的行,只选择了一个符号(从许多符号中)来解析。简单地删除整行,在下面的图片中灰色,使我的代码引用未解析的符号。甚至当我添加回线,它决定没有问题。图。

其他回答

我有这个问题,特别是当我不小心损坏布局或菜单资源文件。我碰到的无法解析符号R的情况是,当我忘记添加“@+id”时,我必须在菜单项中定义一个id。

使用损坏的菜单资源文件,如果我试图用它构建项目,就会得到这个错误。因为R是一个资源的容器类,当gradle不能解析资源的.xml文件时,它似乎不能绑定R。

解决方案是简单地修复资源文件,清理项目并重新构建。

在xml文件中简单地定义一个应用程序名称空间就会导致这种情况。这是一个完全无害的行,只选择了一个符号(从许多符号中)来解析。简单地删除整行,在下面的图片中灰色,使我的代码引用未解析的符号。甚至当我添加回线,它决定没有问题。图。

首先检查是否有任何错误在任何xml布局,如果然后解决它首先。

否则,从项目中删除junit依赖项并重新构建项目。

如果你在.xml文件上有任何错误,无论你做什么,你都无法修复这个问题。我建议

-首先,检查XML文件以查找任何错误和

-秒查找事件日志什么Android工作室捕捉到的错误,它会告诉你哪个文件和错误在哪里。

这就是我解决R问题的方法

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

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

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