我的项目中没有生成 R.java 文件

发布于 2024-09-30 06:21:01 字数 944 浏览 6 评论 0原文

我正在做记事本教程,练习 2。我首先创建一个新的 Android 项目,然后选择从源创建来导入下载的源文件以进行练习。

但是现在我在 Eclipse 中遇到很多错误,问题是没有生成 R.java 类。我该如何解决这个问题?文件夹 gen/ 为空。


我在 Notepadv2.javares/layout/note_edit.xml 上有错误,两者似乎都与生成的 R.java 有关代码> 丢失。

这是我在 Notepadv2.java 中的 import 语句:

import android.R;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter; 

它们是由 Eclipse 命令 Ctrl+Shift+O 创建的。

I am doing the Notepad tutorial, exercise 2. I started by creating a new Android project and chose Create from source to import the downloaded source files for the excercise.

But now I get many errors in Eclipse, and the problem is that there is no generated R.java class. How can I solve this? The folder gen/ is empty.


I have errors on Notepadv2.java and in res/layout/note_edit.xml and both seems to be related to the fact that the generated R.java is missing.

Here is my import statements in Notepadv2.java:

import android.R;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter; 

They are created by the Eclipse command Ctrl+Shift+O.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(21

白云不回头 2024-10-07 06:21:01

转到Project并点击Clean。除其他外,这应该重新生成您的 R.java 文件。

还要删除任何 import android.R.* 语句,然后进行我提到的清理。

显然乔纳斯的问题与不正确的目标构建设置有关。他的目标构建设置为 Android 2.1 (SDK v7),其中布局 XML 使用 Android 2.2 (SDK v8) 元素(布局参数 match_parent) ,因此 Eclipse 无法正确生成 R.java 文件,从而导致了所有问题。

Go to Project and hit Clean. This should, among others, regenerate your R.java file.

Also get rid of any import android.R.* statements and then do the clean up I mentioned.

Apparently Jonas problem was related to incorrect target build settings. His target build was set to Android 2.1 (SDK v7) where his layout XML used Android 2.2 (SDK v8) elements (layout parameter match_parent), due to this there was no way for Eclipse to correctly generate the R.java file which caused all the problems.

白龙吟 2024-10-07 06:21:01

经过一整天的尝试查找为什么没有生成 R 文件的原因,我发现安装 Android SDK Tools r22 后,工具中出现了一个新选项:Android SDK Build Tools。

安装后再次生成R文件。

After one whole day trying to find why R file was not generated, I found that after install Android SDK Tools r22 a new option in Tools was available: Android SDK Build Tools.

After installing it R file was generated again.

北恋 2024-10-07 06:21:01

现在您已经导入了 android.R 而不是您自己的 R...如果您的某个 xml 文件中有错误,请尝试查看您的“问题”视图...摆脱导入 android.R 并注释掉 R 的所有用法。*

当您的项目没有其他错误时,清理应该会有所帮助,因此请检查您的 res 文件夹中的 xml 文件或文件命名

now you have imported android.R instead of your own R... Try to take a look on your "problems" view if you have errors in one of your xml files... get rid of the import android.R and comment out all usages of R.*

Cleaning should help when your project has no other errors, so check your xml files or file naming in your res folders

記憶穿過時間隧道 2024-10-07 06:21:01

可能您使用的 SDK 版本不正确。右键单击您的项目。选择属性-->Android。请注意,2.2 是最新的 SDK。检查它并查看它是否被编译...

编辑

之后也进行清理

Probably u might be using a incorrect SDK version. Right click on your project. Select Properties-->Android. Note that 2.2 is the latest SDK. Check it and see if it gets compiled...

Edit

Also Do a clean after that

薄荷→糖丶微凉 2024-10-07 06:21:01

我遇到了类似的问题

识别此问题的最佳方法是识别 Lint 警告::
*右键单击项目> Android 工具 > 运行 Lint常见错误*

  • 这有助于我们显示一些错误,通过这些错误我们可以修复问题
    这使得 R.java 再次重新生成
  • 通过执行上述步骤,我发现我添加了一些图像
    我没有使用过的文件 ->我删除了它们 ->这修复了
    问题 !

终于清理了项目!

I Had a similar problem

Best way to Identify this problem is to identify Lint warnings::
*Right Click on project > Android Tools > Run Lint : Common Errors*

  • That helps us to show some errors through which we can fix things
    which make R.java regenerated once again
  • By following above steps i identified that i had added some image
    files that i have not used -> I removed them -> That fixed the
    problem !

Finally Clean the project !

旧伤还要旧人安 2024-10-07 06:21:01

我的同事有这个问题 - 重新启动 Eclipse 修复了它。

I colleague of mine has this problem - a restart of eclipse fixed it.

新一帅帅 2024-10-07 06:21:01

将 Android SDK Manager 中的 Android SDK 工具更新为修订版 22.0.1。
这对我有用。

Update Android SDK Tools in Android SDK Manager for revision 22.0.1.
It worked for me.

不羁少年 2024-10-07 06:21:01
  1. 从代码中删除“import R.java”行。它是由 Eclipse 完成的,目的是解决这个问题。
  2. 转到“note_edit.xml”文件。无论您在何处找到“match_parent”作为属性值,请将其替换为“fill_parent”或“wrap_content”。
  3. 进行干净的构建。 R.java 将自动生成。

如果您遇到此问题,则意味着您的内容不支持“match_parent”属性。相反,使用“fill_parent”。

“fill_parent:视图应与其父视图一样大(减去填充)。从 API 级别 8 开始不推荐使用此常量,并由 match_parent 取代。”

  1. Remove the 'import R.java' line from your code. Its done by Eclipse in order to solve the problem.
  2. Go to "note_edit.xml" file. Wherever you find "match_parent" as an attribute value, replace it with either "fill_parent" or "wrap_content".
  3. Do a clean build. R.java will be generated automatically.

If you got this problem, it means "match_parent" property is not supported in your content. Instead, "fill_parent" is used.

"fill_parent: The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent. "

遗心遗梦遗幸福 2024-10-07 06:21:01

你安装了android插件吗?

你的项目必须是android类型的。

Do you have installed the android plugin ?

Your project must be of the android type.

檐上三寸雪 2024-10-07 06:21:01

这实际上是教程代码中的一个错误。我遇到了同样的问题,我终于意识到问题出在“note_edit.xml”文件中。

一些layout_heigh/width属性被设置为“match_parent”,这不是有效值,它们应该被设置为“fill_parent”。

这引发了一个错误,导致 R.java 文件的生成失败。因此,如果您遇到此问题或类似问题,请检查所有 xml 文件并确保其中没有任何错误。

This is actually a bug in the tutorials code. I was having the same issue and I finally realized the issue was in the "note_edit.xml" file.

Some of the layout_heigh/width attributes were set to "match_parent" which is not a valid value, they're supposed to be set to "fill_parent".

This was throwing a bug, that was causing the generation of the R.java file to fail. So, if you're having this issue, or a similar one, check all of your xml files and make sure that none of them have any errors.

挽清梦 2024-10-07 06:21:01

我刚刚遇到一个问题,之前的工作项目停止工作,所有引用 R 的内容都被发布为错误,因为没有生成 R.java。

** * 也检查控制台视图**

我(使用查找器)对主图标进行了备份(甚至没有使用)
所以 res 文件夹之一 (hdpi) 有

icon.png
icon.png 的副本

控制台指示“icon.png 的副本”不是有效的文件名。
其他任何地方都没有标记错误 - res 文件夹中没有红色 X....

但是用“_”替换空格,一切都恢复正常....

I just had a problem where a previously working project stopped working with everything that referenced R being posted as errors because R.java was not being generated.

** * CHECK THE CONSOLE VIEW TOO **

I had (using finder) made a backup of the main icon (not even used)
so one of the res folders (hdpi) had

icon.png
copy of icon.png

Console indicated that "copy of icon.png" was not a valid file name.
No errors were flagged anywhere else - no red X in the res folders....

but replacing the spaces with "_" and it is all back to normal....

漆黑的白昼 2024-10-07 06:21:01

我已经多次遇到过这个问题。根本问题是

  1. 项目设置中未选择目标 sdk 版本

  1. 文件 default.properties(manifest.xml 旁边)丢失或未设置最低 sdk 级别。

检查这两个并按照早期选项中的建议进行干净的构建,它应该可以正常工作。

检查“问题”或“控制台”选项卡中是否有错误。

最后:/eclipse对项目文件夹有写权限吗? (也不太可能的是磁盘空间)

I have faced this issue many times. The root problem being

  1. The target sdk version is not selected in the project settings

or

  1. The file default.properties (next to manifest.xml) is either missing or min sdk level not set.

Check those two and do a clean build as suggested in earlier options it should work fine.

Check for errors in "Problems" or "Console" tab.

Lastly : Do you have / eclipse has write permissions to the project folder? (also less probable one is disk space)

没︽人懂的悲伤 2024-10-07 06:21:01

我还遇到过诸如 R.java 丢失之类的问题,并且当没有错误时 Eclipse 也会抱怨我的代码中存在错误(在类文件中显示红色 X 图标)。

我找到解决此问题的唯一方法是通过选择 Project > 来清理项目。 Clean

这似乎为我自己解决了这个问题,运行 Eclipse 3.5.2

I've also experienced such issues as R.java being missing, and also eclipse complaining there are errors in my code (displaying red X icon against class files) when there were no errors.

The only method I've found for solving this is to clean the project by selecting Project > Clean

This seems to solve the issue for myself, running Eclipse 3.5.2

断桥再见 2024-10-07 06:21:01

是的,我有同样的问题。我解决了
从工具栏中选择项目清理项目
还重置构建自动选项
有时需要更改xml文件的名称匹配格式[az][0-9]
大写字母必须更改

yeah i have the same problem.i fixed it by
selecting Project from the tool bar clean the project
also reset the build automatic option
some times needed to change the name of xml file match format [a-z][0-9]
capital letters must be changed

﹏雨一样淡蓝的深情 2024-10-07 06:21:01

似乎当您更改为最新的 SDK (2.2) 时,构建不再生成 R.java 文件。或者至少不是以我们习惯的方式。要解决此问题,请删除 gen 文件夹下的包并清理而不构建。这为我解决了这个问题。

It seems that when you change to the latest SDK (2.2) building no longer generates an R.java file. Or at least not in the way we're used to. To solve this delete the package underneath the gen folder and clean without building. This solves the issue for me.

辞旧 2024-10-07 06:21:01

由于 Android 插件中的一些内部错误,我遇到了这个问题。当我尝试打开一些布局 xml 时,出现错误:

The project target (Android 2.2) was not properly loaded.

幸运的是,在我的情况下,重新启动 Eclipse 并清理项目有所帮助。

I've got that problem because of some internall error in Android plugin. When I've tried to open some layout xml, I've got error:

The project target (Android 2.2) was not properly loaded.

Fortunatelly in my case restarting Eclipse and cleaning the project helped.

夏夜暖风 2024-10-07 06:21:01

例如,这也可能是 xml 布局文件不正确的问题。如果您的 xml 文件无效(包含错误),则清理后不会重新生成 R 文件。如果修复此问题,R 文件将再次自动生成。

This could also be the problem of an incorrect xml layout file for example. If your xml files are not valid (containing errors) the R file will not re-generate after a clean-up. If you fix this, the R file will be generated automatically again.

笑咖 2024-10-07 06:21:01

根据我的经验我可以总结一下:

1.问题主要是指语法问题,可以
发生在 xml 文件上。

2.您可能不小心将AndroidManifest.xml复制到子文件夹中。

1.一个文件的扩展名未定义。

检查完所有这些后,转到“项目”->“清理”或仅删除 /bin 和 /gen 文件夹。

According to my experince i can summarize :

1.The problem is mainly refering to syntax issues that can be
occured on xml files.

2.You might copy the AndroidManifest.xml accidently to subfolders .

1.One file is an extension undefined .

After checking all of these Go to Project->Clean or just remove the /bin and /gen folders.

爺獨霸怡葒院 2024-10-07 06:21:01

只需重新启动 Eclipse 即可解决此问题,因为工作区将是全新构建的!转到

文件 ->重新启动

这是避免挫败感的最简单方法,进入“构建”、“属性”等等等等......

Just Restart the Eclipse will solve this Issue as the workspace will be freshly Build ! go to

File -> Restart

Its the easiest way to avoid frustration by going it into Build,properties,blah blah.....

深海蓝天 2024-10-07 06:21:01

这是另一个:我正要哭,但还是设法找回了我心爱的 R.java

我已经有一段时间没有更新 Android 安装中的任何内容了。我需要一个功能并运行 SDK 管理器。除了我需要的之外,我基本上检查了它想要更新的所有内容。之后我的 R.java 在 Eclipse 中消失了。

简而言之,在 SDK Manager 中有一个新条目“Android SDK Build-tools”,我想我上次更新时不存在该条目。这确实是一个显而易见的名字。安装完后,Eclipse 将我的 R.java 还给了我。

Here's another one: I was just about to cry, but managed to get my beloved R.java back:

I hadn't updated anything in my Android installation for quite a while. I needed a feature and ran the SDK Manager. Besides what I needed I basically checked everything it wanted to update. Afterwards my R.java disappeared in Eclipse.

To make it short, in the SDK Manager there was a new entry "Android SDK Build-tools" that I guess wasn't there the last time I had updated. It's kind of an obvious name, really. After I installed that one Eclipse gave me my R.java back.

土豪 2024-10-07 06:21:01

java ....而不是调试..
或者
从代码中删除“import R.java”行。它是由 Eclipse 完成的,目的是解决这个问题。
转到“note_edit.xml”文件。无论您在何处找到“match_parent”作为属性值,请将其替换为“fill_parent”或“wrap_content”。
进行干净的构建。 R.java会自动生成

java .... than debug..
or
Remove the 'import R.java' line from your code. Its done by Eclipse in order to solve the problem.
Go to "note_edit.xml" file. Wherever you find "match_parent" as an attribute value, replace it with either "fill_parent" or "wrap_content".
Do a clean build. R.java will be generated automatically

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文