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.


当前回答

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

其他回答

有一件事每次都对我有效:

转到——> build。你项目的gradle (Module: app)文件——>做一点改变(例如:把一个<空格>放在某个地方,然后把它移回来)——>然后android studio会要求你同步你的gradle文件——>在文件的右上角——>选择“立即同步”。

在同步完成后,它将解决大多数情况下的问题。

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

import android.R.*;

首先检查是否有任何错误在任何xml布局,如果然后解决它首先。

否则,从项目中删除junit依赖项并重新构建项目。

我自己也费了好大劲才搞定的。

确保layout.xml文件中没有错误。 转到构建>清洁项目

这招对我管用,希望对你也管用。

你应该做两件事,首先清理项目(在build菜单中)-它会删除build目录,这可能是罪魁祸首:

接下来,同步项目与Gradle文件(在文件下):

这是在Windows 10上的Android Studio 3.6.1中项目的位置。

如果所有这些都失败了,无效缓存和重新启动(在文件下)通常可以做到这一点。在我看来,这将关闭整个程序并占用大量时间。

澄清一下,我是在windows 10上运行这个程序,但它应该也能在MacOS和Linux上运行。