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.


当前回答

显然,在我的例子中,这个问题通过在结尾添加一个“*”得到了解决

import android.R.*;

其他回答

显然,在我的例子中,这个问题通过在结尾添加一个“*”得到了解决

import android.R.*;

只需从根包中导入R符号

import rootpackage.R;

没有重建,同步或其他事情…

在我的情况下,原因是有一些图像在res文件夹名称包含一个大写字母..

只要用小写字母重新命名就可以了

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

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

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

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