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.


当前回答

我也遇到过同样的问题,当我创建一个新项目时,这种情况就会发生。

我所做的是:

检查SDK更新 然后是安卓工作室更新, 然后重新打开项目 打开andoridmanifest.xml 在android:标签中删除“_>”之间的空格并保存。

这对我很有用。

其他回答

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

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

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

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

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

只需清理你的项目和同步项目与Gradle文件。

问题会得到解决。

在我的情况下,代码在我更新android studio之前正常工作。更新后未生成R文件。我正在处理一个自定义视图,并且有一个属性

<attr name="showText" format="boolean" />

在我的declare stylable中。同样的属性也被系统的ui使用:

<style name="Base.Widget.AppCompat.CompoundButton.Switch" parent="android:Widget.CompoundButton">
    <item name="track">@drawable/abc_switch_track_mtrl_alpha</item>
    <item name="android:thumb">@drawable/abc_switch_thumb_material</item>
    <item name="switchTextAppearance">@style/TextAppearance.AppCompat.Widget.Switch</item>
    <item name="android:background">?attr/selectableItemBackgroundBorderless</item>
    <item name="showText">false</item>// this line
</style>

我将之前的showText重命名为showTextFormat,然后gradle sync重新生成R.java文件。

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

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

我在可绘制资源中添加了一些图像,其中一个图像被命名为“super.jpg”,这是一个原因。

重命名图像并在此之后将项目与gradle文件同步修复了错误。