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.
当前回答
我自己也费了好大劲才搞定的。
确保layout.xml文件中没有错误。 转到构建>清洁项目
这招对我管用,希望对你也管用。
其他回答
最近,当我更新一个项目的很多库时,我就遇到了这种情况。没有找到R,但是我可以很好地构建和运行项目。它没有崩溃。IDE只是在所有地方显示红色。
我试过
Check for damaged/improper XMLs and images, none found Check for invalid resource file names (e.g. capitalized), none found Downgrade some libraries because maybe it was a resource conflict, not resolved Lowered compileSdkVersion and targetSdkVersion because, in the past, I encountered resource issues due to this. In this project however, that was not the case. Clean, didn't help Rebuild project, didn't help Invalidate caches/Restart multiple times, didn't help Adjusted idea.max.intellisense.filesize to a very high value, didn't help Did a Lint run, no errors found
最后,我发现降级我的Gradle构建工具(从3.3.1到3.2.1)解决了这个问题。这是我唯一要做的改变。其余内容已更新。现在,我所有的库都更新了,没有问题。
buildscript {
repositories {
google()
jcenter()
}
dependencies {
//classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
我也遇到过类似的问题,我是这样做的:
清洁项目和同步项目与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
只需从根包中导入R符号
import rootpackage.R;
没有重建,同步或其他事情…
清洁,重建和“缓存删除”不会帮助我,更新Android工作室修复了这个问题。
我已经做了其他答案中提到的所有步骤,但它仍然没有得到解决。我正在使用android工作室花栗鼠,最后我能够通过删除来解决这个问题
maven { url "https://jitpack.io" }
从设置。gradle我在回答这个问题,我认为这样做可能会帮助到一些人
推荐文章
- 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?
- Android 4.4 (KitKat)上的Android Gallery为Intent返回不同的URI。ACTION_GET_CONTENT