【问题标题】:Issues with the R.java fileR.java 文件的问题
【发布时间】:2026-02-16 02:45:01
【问题描述】:

当我制作我的 android 应用程序并第一次运行它时,R.java 文件并没有自动生成。然后,我首先清理了项目,然后自动构建它并重新生成了 R.java 文件。但现在我在

上遇到错误
setContentView(R.layout.main);//main can't be resolved or is not a field.

那么,如何克服这个错误,或者告诉一个更好的方法来自动生成 R.java 文件?

【问题讨论】:

  • 检查您是否有import android.R;,如果有,请将其删除。检查资源文件是否有错误。如果是这样,修复它并清理并构建项目
  • @Raghunandan,我已经这样做了,但没有帮助。
  • 如果您的资源文件中有错误,则不会生成 R.java。你检查了吗?如果那没有帮助。祝你好运
  • @Raghunandan 它有效!谢谢老兄。

标签: java android build r.java-file


【解决方案1】:

试试这些东西

如果你的导入中有这个:

import android.R;

删除然后添加

import your.application.packagename.R;

清理并构建项目

查看文档了解详情

http://source.android.com/source/using-eclipse.html

*Note: Eclipse sometimes likes to add an "import android.R" statement at the top of your files that use resources, especially when you ask Eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.*

【讨论】: