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.


当前回答

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

其他回答

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

这个问题似乎有很多原因。最近,我添加了一个名为NewActivity的活动来测试。这创建了一个名为res/menu/new.xml的文件,gradle不喜欢这个名字,因为new是一个保留字。

gradlew戒毒

or

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

这些绝对是值得尝试的好方法,但是在运行这些命令之前,您可能需要解决一些其他问题。

上面的答案对我没有帮助。我最终意识到问题在于Android Studio中缺少“build”文件夹。我在项目“iml”文件中发现了一些行,不包括“build/*”。删除这些错误后,生成文件就出现了,错误就消失了。

您只需要检查Xml文件。 如果出现错误,则无法解决此错误。 只需解决xml错误,然后尝试。

OR

清理你的项目从构建->清洁

如果你在移动java文件或目录到其他位置后看到这个错误,那么你可以保证Android Studio已经混淆了。你猜怎么着?撤销这些操作并不能解决问题。

所以你试着戒毒,但没用。

重启也不行。

但是试试文件->无效缓存/重新启动…->失效并重启

Android Studio维护哪些文件依赖于哪些其他文件的信息。由于移动文件没有正确实现,因此移动文件会导致错误。这还不是全部:这些依赖项的缓存被用于加速构建。

这意味着您不仅必须重新启动,而且还需要使这些缓存失效以恢复(或者更准确地说,重新构建)正常运行。