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.


当前回答

//有时由于不完全同步会产生问题

//所以只要按照步骤,它会做你的工作肯定

Click on Build->Clean Project and that will perform a gradle clean

其他回答

我不得不在android studio中导入我的R包。例如:import com.example。<包名称>。R

在一个非基础特性的即时应用活动中,你可以调用R.id.fromFeatureOne和R.is.fromFeatureTwo。因此,来自不同资源的项目被引用为单一资源来源。

两者都将被高亮显示为可接受的,您甚至可以跳转到源代码。但是构建会导致一个错误:“不能找到符号R.id”,这是相当棘手的原因,因为什么都没有突出显示。

解决方案是调用com.example.feature.one.R.is.fromFeatureOne和com.example.feature.two.R.is.fromFeatureTwo。

在谷歌:https://issuetracker.google.com/u/0/issues/77537714也发布了一个bug

考虑以下IDE功能:

用gradle文件同步项目 Build ->清洁项目 Build ->重建项目 File ->使缓存失效


我的简单解决方案是:

Check that there is no error line in the dependencies of the build.gradle of the app or in all the resource files in the /res directory; where some common errors are when the attribute values are left without references when using: "@string/.." "@style/.." Make sure you are not getting an error of the type Android resource linking failed when the project is rebuilt. Open the file that contains the line with the error Cannot resolve symbol 'R' Scrolling to the last line of the class with said error line Click on a position to the right of the closing key } of the class

通过这种方式,IDE警告对话框将执行r类的导入。

GL

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

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

重启也不行。

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

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

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

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