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文件。 清洁项目。
就是它了。
例如,我在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”时,我必须在菜单项中定义一个id。
使用损坏的菜单资源文件,如果我试图用它构建项目,就会得到这个错误。因为R是一个资源的容器类,当gradle不能解析资源的.xml文件时,它似乎不能绑定R。
解决方案是简单地修复资源文件,清理项目并重新构建。
考虑以下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
在将gradle插件从1.3.1版本升级到
classpath group: 'com.android.tools.build', name: 'gradle', version: '2.2.1'
它迫使我改变了所有的引用,比如
import [applicationId].R;
to
import [package].R;
在我的库项目中,appcontext.getPackageName();停止正常工作。我用它来获得正确的应用程序资源通过反射,但升级gradle插件后,它开始返回特定口味的applicationId,而不是包…你可以在这里阅读更多相关内容。
对于这个问题,我有一个特例。
在我的项目中,一切都很顺利,它可以编译和构建成功,但在我的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 Studio在一开始就会给学习带来麻烦,但就像这里有解决方案清洁项目可以解决这个问题,总是检查包装的名称,不要试图混乱的逻辑,我喜欢软件,但有时我试着思考如果方式是正确的或不正确,我也喜欢Linux,但对我来说,Android Studio重写所有软件也许这不会发生。总之,祝你好运!!!!
推荐文章
- 我可以设置“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?