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中删除了一个项目,比如<Button/>,但它仍然在任何Activity或Fragment .java中声明。 更多。
跟踪错误的最佳方法是重建它,而不是清除或同步一些故意的错误。
如果它不能解决你的问题,那么一定有一些缺陷或运行时错误或错误发生,由于在你的代码或设计中的java或xml文件中的资源使用不当,这迫使gradle停止,因为(R)源文件不能正确索引你的资源,你必须承认这一点。
如果您的项目在进行更改之前已运行,则注释掉所做的更改并尝试重新构建项目。
它肯定会起作用,因为不会有任何变化。
要跟踪准确的错误,可以通过将更改分解为更小的模块来检查更改。
例如,如果您通过单击按钮并在适配器中插入列表值来使列表可见,首先检查是否能够使其可见,然后检查适配器错误。
其他回答
我确实做了一个项目,而且成功了 但在此之前,请尝试检查清单中的包名称,重新构建并清理项目。如果问题没有解决,那就放弃你的项目
我按照上面的方法做了,但不管用。在我的例子中,当我在Android-L预览中每个Java函数都显示为红色时,我不得不设置JDK……
在Mac:
Click once on what is Red (or underlined) in your code (i.e. Activity, String). Click on the Red Light Bulb Point that appears on the left. Select "Setup JDK". Verify if annotations.jar and android.jar are in the current SDK version you are using (my case SDK 20 Android-L). You should also have the "res" folder, but I somehow still had it in the settings. If not, select the SDK and press "-". Then press "+" and locate the SDK folder on your computer (Mac: Applications > Android Studio.app > sdk). Select the version of the SDK you require (in my case 20 Android-L). Press Add. Build and Sync.
然后一切都恢复了正常。
只需进入Android Top菜单列表。点击构建菜单,在构建下点击重建项目。
建立>清洁项目
这对我很管用。同样的问题我也遇到过几次,这似乎解决了问题。除非你改变了一些东西或变量r,这个问题通常是突然发生的,当它发生在我身上时,所以我想这只是Android工作室吓坏了。哈哈
祝你过得愉快,并祝你的项目好运。
在我的情况下,代码在我更新android studio之前正常工作。更新后未生成R文件。我正在处理一个自定义视图,并且有一个属性
<attr name="showText" format="boolean" />
在我的declare stylable中。同样的属性也被系统的ui使用:
<style name="Base.Widget.AppCompat.CompoundButton.Switch" parent="android:Widget.CompoundButton">
<item name="track">@drawable/abc_switch_track_mtrl_alpha</item>
<item name="android:thumb">@drawable/abc_switch_thumb_material</item>
<item name="switchTextAppearance">@style/TextAppearance.AppCompat.Widget.Switch</item>
<item name="android:background">?attr/selectableItemBackgroundBorderless</item>
<item name="showText">false</item>// this line
</style>
我将之前的showText重命名为showTextFormat,然后gradle sync重新生成R.java文件。
推荐文章
- 我可以设置“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?