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.
当前回答
你应该做两件事,首先清理项目(在build菜单中)-它会删除build目录,这可能是罪魁祸首:
接下来,同步项目与Gradle文件(在文件下):
这是在Windows 10上的Android Studio 3.6.1中项目的位置。
如果所有这些都失败了,无效缓存和重新启动(在文件下)通常可以做到这一点。在我看来,这将关闭整个程序并占用大量时间。
澄清一下,我是在windows 10上运行这个程序,但它应该也能在MacOS和Linux上运行。
其他回答
您只需要检查Xml文件。 如果出现错误,则无法解决此错误。 只需解决xml错误,然后尝试。
OR
清理你的项目从构建->清洁
在我的情况下,代码在我更新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文件。
如果你在.xml文件上有任何错误,无论你做什么,你都无法修复这个问题。我建议
-首先,检查XML文件以查找任何错误和
-秒查找事件日志什么Android工作室捕捉到的错误,它会告诉你哪个文件和错误在哪里。
这就是我解决R问题的方法
我受够了
我可绘制资源中的一个大写字母。 导入安卓。R由Android Studio(或Eclipse)添加 xml文件错误
我知道这个问题可能会导致很多事情。在我的例子中,我在其中一个活动*.xml文件中有一些textView。我在编辑器中放入文本(稍后通过代码更改):“NAME: <这里将是名称>”。显然,“<”和“>”字符破坏了在我的项目中从每个地方导入R。我把它换了,它开始工作了。我只是忘记了不能在*.xml文件中使用这些字符,因为它们构建了它的结构。记住这一点是值得的。
推荐文章
- 在应用程序本身中更改Locale
- 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