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.
当前回答
“我也遇到过同样的问题,解决方法是:
1) Sync Project with gradle files
2) Build -> Clean Project
3) Build -> Rebuild Project
4) File -> Invalidate caches
//imp step
5) Check your xml files properly.`
其他回答
我有我的XML文件在一个布局文件夹,而不是菜单文件夹。
这是我们从Eclipse转移到Android Studio的结果。
解决方案对我来说很简单……将XML文件移动到MENU文件夹并重新编译。
造成这个错误的原因有很多。
有时在替换同名的映像文件时会出现这种情况。 假设你从布局。xml中删除了一个项目,比如<Button/>,但它仍然在任何Activity或Fragment .java中声明。 更多。
跟踪错误的最佳方法是重建它,而不是清除或同步一些故意的错误。
如果它不能解决你的问题,那么一定有一些缺陷或运行时错误或错误发生,由于在你的代码或设计中的java或xml文件中的资源使用不当,这迫使gradle停止,因为(R)源文件不能正确索引你的资源,你必须承认这一点。
如果您的项目在进行更改之前已运行,则注释掉所做的更改并尝试重新构建项目。
它肯定会起作用,因为不会有任何变化。
要跟踪准确的错误,可以通过将更改分解为更小的模块来检查更改。
例如,如果您通过单击按钮并在适配器中插入列表值来使列表可见,首先检查是否能够使其可见,然后检查适配器错误。
显然,在我的例子中,这个问题通过在结尾添加一个“*”得到了解决
import android.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.
然后一切都恢复了正常。
建立>清洁项目
这对我很管用。同样的问题我也遇到过几次,这似乎解决了问题。除非你改变了一些东西或变量r,这个问题通常是突然发生的,当它发生在我身上时,所以我想这只是Android工作室吓坏了。哈哈
祝你过得愉快,并祝你的项目好运。
推荐文章
- 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
- 按钮背景是透明的