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.


当前回答

我认为如果你把活动文件放到另一个文件夹而不是默认文件夹。你需要导入com.example.yourproject.R(这是你的项目R文件而不是Android。例如,在MainActivity文件中插入这一行:

import com.example.yourproject.R;

其他回答

我也遇到过同样的问题,当我创建一个新项目时,这种情况就会发生。

我所做的是:

检查SDK更新 然后是安卓工作室更新, 然后重新打开项目 打开andoridmanifest.xml 在android:标签中删除“_>”之间的空格并保存。

这对我很有用。

考虑以下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

我知道这个问题可能会导致很多事情。在我的例子中,我在其中一个活动*.xml文件中有一些textView。我在编辑器中放入文本(稍后通过代码更改):“NAME: <这里将是名称>”。显然,“<”和“>”字符破坏了在我的项目中从每个地方导入R。我把它换了,它开始工作了。我只是忘记了不能在*.xml文件中使用这些字符,因为它们构建了它的结构。记住这一点是值得的。

类型1:关闭项目并删除项目中的.gradle文件 然后重建项目 类型2:如果类型1不起作用,那就把它拿回来 project9Zip文件),然后删除项目。然后提取 压缩项目,只是导入提取的项目,它将工作 很好,因为这对我很管用。

我受够了

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