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.


当前回答

你可以删除一个布局文件(不管哪个),之后你需要构建项目。在我的情况下,如果我试图在删除布局文件之前构建我的项目,它没有帮助我。但是这样也可以。

其他回答

这是最近删除的xml造成的 在Android Studio中,从这些xml中清除你的项目,选择: Build ->清洁项目

我受够了

我可绘制资源中的一个大写字母。 导入安卓。R由Android Studio(或Eclipse)添加 xml文件错误

在一个非基础特性的即时应用活动中,你可以调用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

造成这个错误的原因有很多。

有时在替换同名的映像文件时会出现这种情况。 假设你从布局。xml中删除了一个项目,比如<Button/>,但它仍然在任何Activity或Fragment .java中声明。 更多。

跟踪错误的最佳方法是重建它,而不是清除或同步一些故意的错误。

如果它不能解决你的问题,那么一定有一些缺陷或运行时错误或错误发生,由于在你的代码或设计中的java或xml文件中的资源使用不当,这迫使gradle停止,因为(R)源文件不能正确索引你的资源,你必须承认这一点。

如果您的项目在进行更改之前已运行,则注释掉所做的更改并尝试重新构建项目。

它肯定会起作用,因为不会有任何变化。

要跟踪准确的错误,可以通过将更改分解为更小的模块来检查更改。

例如,如果您通过单击按钮并在适配器中插入列表值来使列表可见,首先检查是否能够使其可见,然后检查适配器错误。

只需进入Android Top菜单列表。点击构建菜单,在构建下点击重建项目。