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 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,红色消失,我又可以愉快地编程了。

其他回答

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

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

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

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

做:建立>清洁项目

重要:确保在消息窗格(Alt+0)中清除项目后没有任何错误。如果您发现任何带有感叹号的红色圆圈,那么您必须删除代码中的这些错误。

这些错误大多与@string/或@array/有关。再次清洁项目,完成!

我也遇到过类似的问题,我是这样做的:

清洁项目和同步项目与Gradle,

检查buildTools版本在我的sdk

从build gradle (module)中将minSdkVersion从8修改为9

defaultConfig {
        applicationId "PackageName"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }

但这一切都无济于事。

最后我找到了答案(这对我来说很管用)

从build.gradle(module:app)更改

android {
    compileSdkVersion 21
    buildToolsVersion '21.0.1'
......
....
}

Or

选择文件|项目结构将构建工具版本更改为21.1.1

检查xml文件。 清洁项目。

就是它了。

例如,我在strings.xml中有一个条目:

<string name="A">Some text</string>

在activity_main.xml中,我使用了这个字符串条目

<TextView
android:id="@+id/textViewA"
android:text="@string/A"/>

当我一直在做这个项目的时候,我把它删除了

 <string name="A">Some text</string>

因此activity_main.xml就无效了。结果:不能解析R。

因此,检查您的*.xml文件并清除项目。