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.
当前回答
我也遇到过同样的问题,当我创建一个新项目时,这种情况就会发生。
我所做的是:
检查SDK更新 然后是安卓工作室更新, 然后重新打开项目 打开andoridmanifest.xml 在android:标签中删除“_>”之间的空格并保存。
这对我很有用。
其他回答
在一个非基础特性的即时应用活动中,你可以调用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
你可以删除一个布局文件(不管哪个),之后你需要构建项目。在我的情况下,如果我试图在删除布局文件之前构建我的项目,它没有帮助我。但是这样也可以。
此问题是由于“res”或“drawable”文件夹中的文件命名引起的。不能使用任何大写字母来命名资源文件。因此,检查是否有这样的文件,并用小写字母重命名它。
你也可以试试这个:转到: 文件>使缓存失效/重新启动
这些选择对我很有效。
对我来说,问题是我升级到一个似乎有一些bug的Gradle版本。3.4.0-alpha02是我更新到的,发现这是“无法解析符号R”错误的罪魁祸首。我恢复到3.3.0-alpha10(即类路径'com.android.tools.build:gradle:3.3.0-alpha10'),它解决了这个问题。
这显然不是一个持久的解决方案,因为我迟早需要升级,但截至2018年11月3日,3.4.0-alpha02是最新版本,它阻碍了我的开发,因为它不允许我通过单击R引用在IDE中跳跃。
推荐文章
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- Android Studio中的。iml文件是什么?
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器