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.


当前回答

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

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

其他回答

我按照上面的方法做了,但不管用。在我的例子中,当我在Android-L预览中每个Java函数都显示为红色时,我不得不设置JDK……

在Mac:

Click once on what is Red (or underlined) in your code (i.e. Activity, String). Click on the Red Light Bulb Point that appears on the left. Select "Setup JDK". Verify if annotations.jar and android.jar are in the current SDK version you are using (my case SDK 20 Android-L). You should also have the "res" folder, but I somehow still had it in the settings. If not, select the SDK and press "-". Then press "+" and locate the SDK folder on your computer (Mac: Applications > Android Studio.app > sdk). Select the version of the SDK you require (in my case 20 Android-L). Press Add. Build and Sync.

然后一切都恢复了正常。

你们最近更新了SDK工具吗?启动android SDK管理器,确保你有最新的SDK工具,现在是独立于平台工具。当我第一次更新我的SDK管理器时,我也遇到了同样的问题,SDK构建工具包在安装/更新时没有出现,直到我关闭并重新打开SDK管理器。

其中一个原因可能是,你最近修改了布局/资源文件,资源(字符串/颜色/可绘制等)在你的代码中不可用,但你在设计中使用了参考。

试着寻找这样的引用,删除/注释它们,并尝试重新构建。

我最近遇到了这个问题,清洁/构建等都没有解决它。我猜我在某个地方有一个问题,但不确定在哪里找到它(它是在清单,资源xml文件等?)

当出现问题时,您可以通过运行代码分析来找出问题所在。

从顶部的菜单中选择:

检查代码,使用'Default'配置文件扫描整个项目。

您的检查结果将包含一个名为“Android”的元素。在这里,您将获得导致资源和清单文件中出现问题的所有内容的列表。这些错误将打开生成的xml文件,并以红色显示任何错误。

我的确切问题是由于我使用基于flavor的清单文件,并且我意外地在flavor中的<application>标签内复制了一些权限。

更新:我确实有一个问题后,Android工作室仍然显示错误后修复,即使我现在可以构建和运行完美,而错误仍然存在。(我正在运行一个金丝雀构建,所以现在把它放下来)

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