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”的错误。 试试这些解决方案。

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

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

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

感谢阅读。

其他回答

我确实做了一个项目,而且成功了 但在此之前,请尝试检查清单中的包名称,重新构建并清理项目。如果问题没有解决,那就放弃你的项目

这个符号看起来很好用。

android:id="@+id/viewID"

Android Studio的设计面板似乎不太好用。

在我的情况下,我需要修复我的布局XML文件中的一些错误。没有指示文件包含错误的图标。然而,当我点击它们时,我注意到一些红线。修好后,找到了R。

我也遇到过同样的问题,大多数时候都是通过

同步项目与gradle文件 做构建->清洁项目 Doing File ->使缓存失效

但这一次,即使在做了所有这些事情后,错误仍然存在,最后我找到了罪魁祸首。

问题是在一个xml文件,在那里我给了一个普通的字符串值为android:id而不是id资源类型。

错误

android:id="username"

修正

android:id="@id/username"

混淆xml文件中与资源相关的内容是一个主要问题 此错误的原因。注意,它可能不会在xml布局文件中显示为错误。

请注意

在大多数情况下,编译器会在消息中显示错误的来源。 在尝试其他解决方案之前,请先检查它

在我的例子中,导入应用程序域名后跟“。R解决了这个问题。例子:

import com.example.myapp.R;