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.


当前回答

显然,在我的例子中,这个问题通过在结尾添加一个“*”得到了解决

import android.R.*;

其他回答

在我的例子中,导入应用程序域名后跟“。R解决了这个问题。例子:

import com.example.myapp.R;

如果你在移动java文件或目录到其他位置后看到这个错误,那么你可以保证Android Studio已经混淆了。你猜怎么着?撤销这些操作并不能解决问题。

所以你试着戒毒,但没用。

重启也不行。

但是试试文件->无效缓存/重新启动…->失效并重启

Android Studio维护哪些文件依赖于哪些其他文件的信息。由于移动文件没有正确实现,因此移动文件会导致错误。这还不是全部:这些依赖项的缓存被用于加速构建。

这意味着您不仅必须重新启动,而且还需要使这些缓存失效以恢复(或者更准确地说,重新构建)正常运行。

R.java文件包含xml和java页面之间的链接。只有在某些资源文件出现问题时,才会出现“R无法解决”。因此,最好和有效的方法是删除最后完成的xml或绘制在res文件。然后再根据android编码规范从那里开始。这是唯一的办法。有效而正确的方法。

也可以使用Git来正确地跟踪代码。

这些在Ubuntu 12.04上都不适用。我必须做到以下几点:

sudo apt-get install lib32stdc++6
sudo apt-get install lib32z1

在那之后,我遵循了这个线程中的其他一些建议,做了Build ->清洁项目,一切都很好。

我也有这个问题。简单的“gradlew clean”和“gradlew build”就成功了。


点击Build->Clean Project,将执行gradle Clean