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工作室修复了这个问题。

其他回答

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

在我的项目中,一切都很顺利,它可以编译和构建成功,但在我的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,红色消失,我又可以愉快地编程了。

看看Gradle控制台。在我的例子中,我包含了一个文件扩展名错误的资源。

AAPT: libpng error: Not a PNG file
:app:mergeDebugResources FAILED

FAILURE: Build failed with an exception.

在重命名文件和同步项目与Gradle文件之后,一切都很好。

我也有这个问题。简单的“gradlew clean”和“gradlew build”就成功了。


点击Build->Clean Project,将执行gradle Clean

在我的案例中,通过将Android Studio从3.1更新到3.3解决了这个问题

要更新您的Android Studio:文件->设置->系统设置->更新,然后点击立即检查。

我有我的XML文件在一个布局文件夹,而不是菜单文件夹。

这是我们从Eclipse转移到Android Studio的结果。

解决方案对我来说很简单……将XML文件移动到MENU文件夹并重新编译。