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.
当前回答
The above answers are also the ways to fix the error but if does not work for anyone then i have an option...
只需要关闭项目,然后重新启动它,这样它就会加载引用… 跟着截图走。
其他回答
只需清理你的项目和同步项目与Gradle文件。
问题会得到解决。
同样的问题。当我在我的可绘制文件夹中添加了一些图像并试图访问它们时开始。此外,添加的图像有大写字母的扩展名。这似乎使构建崩溃,因为即使我重命名它们,消息也是一样的,R是不可访问的。我所做的是,在.iml文件中,我寻找了excludeFolder并删除了它们(如下图所示):
<excludeFolder url="file://$MODULE_DIR$/build/apk" />
<excludeFolder url="file://$MODULE_DIR$/build/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
<excludeFolder url="file://$MODULE_DIR$/build/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/res" />
<excludeFolder url="file://$MODULE_DIR$/build/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
在那之后,我重建了项目,R神奇地重新出现了。
我不得不在android studio中导入我的R包。例如:import com.example。<包名称>。R
还有一件事要尝试……
删除Build文件夹 通过同步gradle重新构建项目
我已经试过很多其他的建议了,比如
清洁项目, 重新启动Android Studio, 修复错误在我的xml, 同步项目与Gradle文件,以及 更新构建工具版本,
但这些都不起作用。
我最终手动删除了整个构建文件夹(在备份我的项目后),然后重新启动Android Studio。之后,我在我的应用程序构建中做了一个小编辑(添加和删除一个空格)。Gradle文件,然后点击同步。(我可能只需要从菜单中使用Gradle文件完成Tools > Android > Sync Project。)从那以后,它起作用了。在构建文件夹中已经损坏了一些东西,并开始与一个新的构建修复它。
我不知道有没有人读到这么远的答案,但我还是要加上这个答案。
你们最近更新了SDK工具吗?启动android SDK管理器,确保你有最新的SDK工具,现在是独立于平台工具。当我第一次更新我的SDK管理器时,我也遇到了同样的问题,SDK构建工具包在安装/更新时没有出现,直到我关闭并重新打开SDK管理器。
推荐文章
- 我可以设置“android:layout_below”在运行时编程?
- Android Studio谷歌JAR文件导致GC开销限制超过错误
- 为什么每当我创建一个新项目时,Eclipse都会自动添加appcompat v7库支持?
- 如何同步项目到GitHub与Android工作室?
- Keytool错误:java.io。IoException:错误的AVA格式
- GCM与PHP(谷歌云消息传递)
- 自定义圆圈按钮
- 我如何使用一个复合绘图而不是一个线性布局,包含一个ImageView和一个TextView
- 如何绘制一个线性布局的边界
- 如何动画添加或删除Android列表视图行
- 如何解决“丢失的PendingIntent可变性标志”在android api 30+ lint警告?
- 我怎么能修复“意外元素<查询>发现<manifest>”错误?
- Android房间-自动生成新插入行的id
- Android Studio在哪里保存ProGuard映射文件?
- 我如何从资产文件夹解析一个本地JSON文件到一个ListView?