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.


当前回答

“我也遇到过同样的问题,解决方法是:

1) Sync Project with gradle files
2) Build -> Clean Project
3) Build -> Rebuild Project
4) File -> Invalidate caches

//imp step
5) Check your xml files properly.`

其他回答

在新的android studio 2.2中。如果你的TargetSDK =< 15,那么你也可能会发现这个问题。在gradle中改变这个。构建,然后按照前面的答案执行步骤。同步gradle文件并重建。您还可以使缓存无效并重新启动工作室,这有时也有帮助。

你们最近更新了SDK工具吗?启动android SDK管理器,确保你有最新的SDK工具,现在是独立于平台工具。当我第一次更新我的SDK管理器时,我也遇到了同样的问题,SDK构建工具包在安装/更新时没有出现,直到我关闭并重新打开SDK管理器。

检查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文件并清除项目。

我确实做了一个项目,而且成功了 但在此之前,请尝试检查清单中的包名称,重新构建并清理项目。如果问题没有解决,那就放弃你的项目

在我的情况下:res/someLayout.xml文件出错,然后我解决它。 然后清除该项目。 错误消失了。