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.


当前回答

我在androidmanifest中有android:textAllCaps = "false",这对R文件造成了严重破坏。我删除了,一切正常。我认为目标sdk设置得不够高。

其他回答

还有一件事要尝试……

删除Build文件夹 通过同步gradle重新构建项目

我已经试过很多其他的建议了,比如

清洁项目, 重新启动Android Studio, 修复错误在我的xml, 同步项目与Gradle文件,以及 更新构建工具版本,

但这些都不起作用。

我最终手动删除了整个构建文件夹(在备份我的项目后),然后重新启动Android Studio。之后,我在我的应用程序构建中做了一个小编辑(添加和删除一个空格)。Gradle文件,然后点击同步。(我可能只需要从菜单中使用Gradle文件完成Tools > Android > Sync Project。)从那以后,它起作用了。在构建文件夹中已经损坏了一些东西,并开始与一个新的构建修复它。

我不知道有没有人读到这么远的答案,但我还是要加上这个答案。

此问题是由于“res”或“drawable”文件夹中的文件命名引起的。不能使用任何大写字母来命名资源文件。因此,检查是否有这样的文件,并用小写字母重命名它。

你也可以试试这个:转到: 文件>使缓存失效/重新启动

这些选择对我很有效。

在最新版本的Android Studio中,至少对我来说,如下作品:

“工具”->“Android”->“同步项目与Gradle文件”

在最新的Android Studio 3.1.3(2018年7月)中,“同步项目与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文件中的一些错误。没有指示文件包含错误的图标。然而,当我点击它们时,我注意到一些红线。修好后,找到了R。