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.
当前回答
我认为如果你把活动文件放到另一个文件夹而不是默认文件夹。你需要导入com.example.yourproject.R(这是你的项目R文件而不是Android。例如,在MainActivity文件中插入这一行:
import com.example.yourproject.R;
其他回答
我也遇到过类似的问题,我是这样做的:
清洁项目和同步项目与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
检查xml文件。 清洁项目。
就是它了。
例如,我在strings.xml中有一个条目:
<string name="A">Some text</string>
在activity_main.xml中,我使用了这个字符串条目
<TextView
android:id="@+id/textViewA"
android:text="@string/A"/>
当我一直在做这个项目的时候,我把它删除了
<string name="A">Some text</string>
因此activity_main.xml就无效了。结果:不能解析R。
因此,检查您的*.xml文件并清除项目。
我确实做了一个项目,而且成功了 但在此之前,请尝试检查清单中的包名称,重新构建并清理项目。如果问题没有解决,那就放弃你的项目
你们最近更新了SDK工具吗?启动android SDK管理器,确保你有最新的SDK工具,现在是独立于平台工具。当我第一次更新我的SDK管理器时,我也遇到了同样的问题,SDK构建工具包在安装/更新时没有出现,直到我关闭并重新打开SDK管理器。
这个问题似乎有很多原因。最近,我添加了一个名为NewActivity的活动来测试。这创建了一个名为res/menu/new.xml的文件,gradle不喜欢这个名字,因为new是一个保留字。
gradlew戒毒
or
“工具”->“Android”->“同步项目与Gradle文件”
这些绝对是值得尝试的好方法,但是在运行这些命令之前,您可能需要解决一些其他问题。
推荐文章
- 我可以设置“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?