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版本0.8.9出现相同的错误(无法解析符号R)后,我所做的是去文件->项目结构,并将JDK的位置更改为我手动安装的oracle JDK。在我的例子中,在linux中,而不是预设的/usr/lib/jvm/java-7-oracle。我将其更改为/home/phvz/App/jdk1.8.0_20保存了设置,R符号问题立即得到了修复
Build ->清理项目或重建项目 或 File -> Invalidate cache / Restart…->失效并重启
我也有同样的问题: Android Studio 3.2 该项目编译和运行良好,但我在屏幕上得到“无法解析符号“R”的警告
没有帮助:
构建——>清洁项目 构建- >重建项目 文件- >缓存失效/重新启动
帮助:
要么
在build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
...
}
替换为
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
...
}
or
更新Android Studio到3.3
检查xml文件。 清洁项目。
就是它了。
例如,我在strings.xml中有一个条目:
<string name="A">Some text</string>
在activity_main.xml中,我使用了这个字符串条目
<TextView
android:id="@+id/textViewA"
android:text="@string/A"/>
当我一直在做这个项目的时候,我把它删除了
<string name="A">Some text</string>
因此activity_main.xml就无效了。结果:不能解析R。
因此,检查您的*.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
推荐文章
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的