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.
当前回答
//有时由于不完全同步会产生问题
//所以只要按照步骤,它会做你的工作肯定
Click on Build->Clean Project and that will perform a gradle clean
其他回答
其中一个原因可能是,你最近修改了布局/资源文件,资源(字符串/颜色/可绘制等)在你的代码中不可用,但你在设计中使用了参考。
试着寻找这样的引用,删除/注释它们,并尝试重新构建。
在xml文件中简单地定义一个应用程序名称空间就会导致这种情况。这是一个完全无害的行,只选择了一个符号(从许多符号中)来解析。简单地删除整行,在下面的图片中灰色,使我的代码引用未解析的符号。甚至当我添加回线,它决定没有问题。图。
对于这个问题,我有一个特例。
在我的项目中,一切都很顺利,它可以编译和构建成功,但在我的Android Studio IDE(我也尝试了Intelligent IDEA,它们是在相同的情况下),R.java文件不能很好地解析,总是掉在红线中。
就像这样:
这几乎让我疯了,我不能忍受读取颜色当我编程。
最后我发现这个棘手的问题是由我的R.java的文件大小引起的。 我的项目非常庞大,它支持多种语言和多种屏幕尺寸。资源太多了,我的R.java文件大小大约是2.5M。
Android Studio中文件的最大大小默认设置为2.5M,因此大于这个限制的文件不能很好地解析。您可以在“AndroidStudio-root/bin/idea.properties”中更改默认设置。
更改这一行:
idea.max.intellisense.filesize=2500
to :
idea.max.intellisense.filesize=5000
然后重新启动Android studio,红色消失,我又可以愉快地编程了。
只需进入Android Top菜单列表。点击构建菜单,在构建下点击重建项目。
检查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文件并清除项目。
推荐文章
- 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
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?