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文件中有一些textView。我在编辑器中放入文本(稍后通过代码更改):“NAME: <这里将是名称>”。显然,“<”和“>”字符破坏了在我的项目中从每个地方导入R。我把它换了,它开始工作了。我只是忘记了不能在*.xml文件中使用这些字符,因为它们构建了它的结构。记住这一点是值得的。
其他回答
在我的情况下,原因是有一些图像在res文件夹名称包含一个大写字母..
只要用小写字母重新命名就可以了
在我的情况下,我试图清理和重建项目。这个问题没有修复,因为在布局文件中存在错误。为了重新创建,我在应用程序名称空间的属性上添加了一个错字。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/add_user"
android:icon="@android:drawable/ic_menu_add"
android:title="Add User"
app:showAsActioin="always"
/>
</menu>
Android工作室没有检查应用程序名称空间的属性,似乎文件是无错误的。现在,在构建项目时,它说无法解析符号“R”。错误显示在构建日志中。
修复错字修复它,手表上的Android问题部分的构建日志。
做:建立>清洁项目
重要:确保在消息窗格(Alt+0)中清除项目后没有任何错误。如果您发现任何带有感叹号的红色圆圈,那么您必须删除代码中的这些错误。
这些错误大多与@string/或@array/有关。再次清洁项目,完成!
我也遇到过类似的问题,我是这样做的:
清洁项目和同步项目与Gradle,
检查buildTools版本在我的sdk
从build gradle (module)中将minSdkVersion从8修改为9
defaultConfig {
applicationId "PackageName"
minSdkVersion 9
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
但这一切都无济于事。
最后我找到了答案(这对我来说很管用)
从build.gradle(module:app)更改
android {
compileSdkVersion 21
buildToolsVersion '21.0.1'
......
....
}
Or
选择文件|项目结构将构建工具版本更改为21.1.1
这些在Ubuntu 12.04上都不适用。我必须做到以下几点:
sudo apt-get install lib32stdc++6
sudo apt-get install lib32z1
在那之后,我遵循了这个线程中的其他一些建议,做了Build ->清洁项目,一切都很好。
推荐文章
- 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
- 按钮背景是透明的