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.


当前回答

你的代码被打乱了。答案相当简单。

点击构建->清洁项目。

这样应该可以了。

检查你的Android Studio版本。

我目前使用的是Android Studio 3.0.1。

其他回答

以上答案都不能解决我的问题。所以我添加了我的解决方案,以防有人遇到类似的问题。在我的构建中。我有gradle依赖,

compile 'com.android.support:appcompat-v7:22+'

我的compilesdk版本是 编译esdkversion 21,所以android工作室不高兴。我简单地更新了我的compilesdkversion到23,并进行了同步。

我还将依赖项更新为

compile 'com.android.support:appcompat-v7:23.1.1'

匹配我的支持库在工具—> Android—> SDK管理器—> SDK工具选项卡—> Android支持库—>版本。 现在一切都好了。快乐编码:)

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

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

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

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

错误

android:id="username"

修正

android:id="@id/username"

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

请注意

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

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

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

在最新的Android Studio 3.1.3(2018年7月)中,“同步项目与Gradle文件”在主菜单栏中可用。

对我来说,重要的是说Android Studio在一开始就会给学习带来麻烦,但就像这里有解决方案清洁项目可以解决这个问题,总是检查包装的名称,不要试图混乱的逻辑,我喜欢软件,但有时我试着思考如果方式是正确的或不正确,我也喜欢Linux,但对我来说,Android Studio重写所有软件也许这不会发生。总之,祝你好运!!!!

对于这个问题,我有一个特例。

在我的项目中,一切都很顺利,它可以编译和构建成功,但在我的Android Studio IDE(我也尝试了Intelligent IDEA,它们是在相同的情况下),R.java文件不能很好地解析,总是掉在红线中。

就像这样:

这几乎让我疯了,我不能忍受读取颜色当我编程。

最后我发现这个棘手的问题是由我的R.java的文件大小引起的。 我的项目非常庞大,它支持多种语言和多种屏幕尺寸。资源太多了,我的R.java文件大小大约是2.5M。

Android Studio中文件的最大大小默认设置为2.5M,因此大于这个限制的文件不能很好地解析。您可以在“AndroidStudio-root/bin/idea.properties”中更改默认设置。

更改这一行:

idea.max.intellisense.filesize=2500

to :

idea.max.intellisense.filesize=5000

然后重新启动Android studio,红色消失,我又可以愉快地编程了。