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.
当前回答
我按照上面的方法做了,但不管用。在我的例子中,当我在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.
然后一切都恢复了正常。
其他回答
Build ->清理项目或重建项目 或 File -> Invalidate cache / Restart…->失效并重启
我正在做一个多模块的项目。这发生在我把所有ui代码移到一个新模块时。
下面是我解决这个问题的方法: 我发现当我创建新的ui模块时,在/res/values中,它创建了一个新的strings.xml,其中没有任何有用的东西。 我删除了strings.xml文件,重新构建项目,它工作了。
清洁,重建和“缓存删除”不会帮助我,更新Android工作室修复了这个问题。
The above answers are also the ways to fix the error but if does not work for anyone then i have an option...
只需要关闭项目,然后重新启动它,这样它就会加载引用… 跟着截图走。
尝试确保完整的包名在第一行。假设您有一个名为LoginActivity.java的文件和一个名为com.domainname.project的包。
java的第一行应该是
package com.domainname.project;
这相当于在dot.net中指定“namespace”。还要确保该包与包含*.java文件的包匹配。
推荐文章
- 在Android中,对话框被取消或被取消有什么区别?
- 在ScrollView触摸处理中的HorizontalScrollView
- 何时在Android中使用RxJava,何时使用Android架构组件中的LiveData ?
- 如何在Android项目中使用ThreeTenABP
- 指定的子节点已经有一个父节点。你必须先在子对象的父对象上调用removeView() (Android)
- 我的Android设备没有出现在adb设备列表中
- 在没有安装apk的情况下获取Android .apk文件的VersionName或VersionCode
- Fragment onResume() & onPause()不会在backstack上被调用
- 如何设置基线对齐为假提高性能在线性布局?
- 如何获得当前屏幕方向?
- 如何在Android中渲染PDF文件
- 我如何解决错误“minCompileSdk(31)指定在一个依赖的AAR元数据”在本机Java或Kotlin?
- 如何改变TextInputLayout的浮动标签颜色
- Android工作室如何运行gradle同步手动?
- 如何以编程方式在我的EditText上设置焦点(并显示键盘)