android 缺少 R 文件

发布于 2024-10-14 17:10:21 字数 217 浏览 4 评论 0原文

我使用的是 Eclipse Helios,但由于性能问题,我更改为 Eclipse Galileo 并安装了 ADT 插件,并将我的 sdk 文件夹添加到 Elcipse Preferences 中。现在 R.java 从我的所有项目中消失了。我该如何解决这个问题,我执行了 Project/Clean 但不会生成文件,我的 xml 中没有任何问题,并且我的任何项目中都没有 out.xml 。

提前致谢

i was using Eclipse Helios but due to performance issues i changed to Eclipse Galileo and installed the ADT plugging, and added my sdk folder to Elcipse Preferences. Now R.java disappeared from all of my projects. How can i fix this, i did Project/Clean but that won't generate the files, there's nothing wrong in my xml and there's no out.xml in any of my projects.

Thanks in advance

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

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

发布评论

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

评论(14

咋地 2024-10-21 17:10:21

根据我的经验,xml 中某处有错误。 Eclipse 无疑会告诉您在哪里。修复后,出现错误 R。

In my experience there is a mistake in the xml somewhere. Eclipse will no doubt tell you where. After I fix the error R appears.

伴我老 2024-10-21 17:10:21

有时,当您清理项目时,R 文件会消失,我也遇到了同样的问题。

我修复它的方法是:

-确保所有“导入 android.R”都被删除
- 再次清理(如果这不能解决问题,请重新启动 Eclipse 并重试)

- 将指向 R 文件的指针放在注释 fe // setContentView(R.layout.main); 中
-如果所有指向 R 文件的指针都在注释中,您应该只在文件中收到警告,并希望在其他地方收到错误。
- 修复错误,然后取消注释指针。有时 eclipse 会忽略一些错误并删除 R 文件,然后什么也不说,这很烦人,但这会修复它。 :)

-如果这仍然不起作用,您可以尝试创建一个新项目并将代码复制粘贴到其中。

Sometimes when you clean your project the R file disappears, I had the same issue.

The way I fixed it was:

-Make sure all the "import android.R" was removed
-Clean again (if this doesn't fix it, restart eclipse and try again)

Or

-Put the pointers to R file in comment f.e. // setContentView(R.layout.main);
-If all the pointers to R file are in comment, you should get only warnings in the file, and hopefully errors somewhere else.
-Fix the errors and then uncomment the pointers. Sometimes eclipse ignores some errors and drops the R file and then says nothing about it, which is annoying, but this will fix it. :)

-If this still doesn't work, you can try create a new project and copy paste your code in it.

小鸟爱天空丶 2024-10-21 17:10:21

我今天遇到了同样的问题并解决了。在包含外部/示例文件时经常发生这种情况的原因是,这些示例经常引用应用程序中的布局,但无权访问该包,因此无法看到该包中的 R.java 文件。为了清楚起见,这是 R.java 文件的开头:

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.conceptualsystems.dashboard;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int csc_logo=0x7f020000;
        public static final int icon=0x7f020001;
    }
    public static final class id {
        public static final int activation_code=0x7f070012;
        public static final int alpha_bar=0x7f07000b;
        public static final int alpha_label=0x7f07000a;

注意包名称是应用程序的包名称。未包含在该包中的 .java 文件(即您刚刚放入的示例代码)将需要显式引用该包文件,如下所示:

package com.example.android.apis.graphics;

import android.app.Dialog;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import com.conceptualsystems.dashboard.R;

最后一行是需要注意的一行。显式导入资源后,这些资源将在您的示例代码中可用。

I had this same issue today and figured it out. The reason this happens so often when including external/example files is because often times these examples reference layouts in your application, but do not have access to the package and therefore cannot see the R.java file in that package. to make things clear, here's the beginning of the R.java file:

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.conceptualsystems.dashboard;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int csc_logo=0x7f020000;
        public static final int icon=0x7f020001;
    }
    public static final class id {
        public static final int activation_code=0x7f070012;
        public static final int alpha_bar=0x7f07000b;
        public static final int alpha_label=0x7f07000a;

notice the package name is whatever the package name of your application is. .java files that are not included in this package (ie, your example code you just dropped in) will need to explicitly reference that package file like this:

package com.example.android.apis.graphics;

import android.app.Dialog;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import com.conceptualsystems.dashboard.R;

the last line is the one to pay attention to. after importing the resources explicitly, the resources will be available in your example code.

小姐丶请自重 2024-10-21 17:10:21

在 eclipse 中创建了新项目,然后我将示例文件导入其中,但仍然出现相同的错误。您可能想尝试以下操作:

  • 在 Eclipse 中 ->右键单击项目 -> Android 工具 -> 在 Eclipse 中修复项目

  • 确保所有资源均为小写。

created new project in eclipse, then I imported the sample files into it, yet still same error. You might want to try the following:

  • In Eclipse -> Right Click on Project -> Android Tools -> Fix Project

  • made sure that all resources are lowercase.

泪眸﹌ 2024-10-21 17:10:21

尝试在项目下拉列表中取消单击并重新单击自动构建项目,每次都关闭程序。关于此问题的已知错误已经有一段时间了,但并不总是有固定的修复。

当我遇到这种情况时,结果发现这是因为我用大写字母命名了 .xml 文件。值得检查。

Try unclicking and re-clicking build project automatically in the project dropdown, closing the program each time. There have been know bugs about this for a while but there's not always a set fix.

When it once happened to me it turned out it was because i'd named an .xml file with a capital letter. Worth checking.

熊抱啵儿 2024-10-21 17:10:21

我在从 Eclipse/Helios 升级到 Eclipse/Juno 时遇到了这个问题。

我设置了 IDE,然后导入了现有(工作)项目。如果我然后在模拟器中运行它,它会运行,但是是原始版本,尽管 java 代码是最新版本的。

然后,我进行了清理(如许多其他帖子中所述)和(重新)构建,但由于缺少 R.java 文件,最终出现了许多错误。

问题原来是布局 xml 文件中的一些“错误”未被 Eclipse/Helios 识别。具体来说,就我而言,Juno 反对一些layout_width='match-parent'(和高度)设置。我尝试直接在 xml 文件中更改这些内容,但 Juno 没有它,并且仍然将其报告为“匹配父项”错误(尽管它显然已被更改)。

唯一要做的就是转到图形布局,右键单击以获取属性,然后更改其中的宽度和高度属性。另外,由于它们已经显示为“fill-parent”,我必须首先将它们更改为“wrap-content”,然后将它们更改回“fill-parent”。

一旦我对“match-parent”的所有实例完成此操作,我就可以进行构建并在模拟器中运行最新版本。

I had this problem when upgrading from Eclipse/Helios to Eclipse/Juno.

I set up the IDE and then imported an existing (working) project. If I then ran it in the emulator it ran but in its original version although the java code was for the latest version.

I then did a Clean (as explained in many other posts) and a (re) build but then ended up with host of errors due toSmissing R.java file.

The problem turned out to be some 'errors' in the layout xml files which had not been recognised by Eclipse/Helios. Specifically, in my case, there were some layout_width='match-parent' (and height) settings which Juno objected to. I tried changing those directly in the xml files but Juno wasn't having it and still reported it as a 'match-parent' error (although it had clearly been changed).

The only thing to do was to go to the graphical layout, right-click to get Properties and then change the Width and Height properties there. Also, as they were already showing as 'fill-parent', I had to first change them to 'wrap-content' and then change them back to 'fill-parent'.

Once I'd done that for all instances of 'match-parent' I could then do a build and run the latest version in the emulator.

装纯掩盖桑 2024-10-21 17:10:21

res 中有错误。一旦我在布局和菜单中有相同的 xml 文件,生成失败,没有生成 R.java。

There are mistakes in res. Once I have the same xml files in layout and menu, generate failed, no R.java generated.

碍人泪离人颜 2024-10-21 17:10:21

本地化正确的 R. 包导入,应该在您的包中。

前任。 导入 com.yourdomain.test.R;

Localize the correct R. package import, should be in your packages.

ex. import com.yourdomain.test.R;

埖埖迣鎅 2024-10-21 17:10:21

这就是 ADT Android 项目与普通 Java 项目有点不同的地方。 Android 项目需要一个不断更新的持久 R.java 文件。除此之外,R.java 文件具有应用程序中提供的所有资源 ID,并保持界面的一致性。

因此,首先确保为您的 Android 项目生成 R.java 文件。

  • 要找到这一点,首先转到 ADT (Eclipse) 的顶部并清理项目 PROJECT->CLEAN
  • 如果选中了 'Build Automatically',则 R.java 文件现在应该创建。
  • 要进行确认,您只需在项目文件夹中搜索该文件即可。如果它没有被创建,你将找不到它。
  • 如果未创建 R.java,则您的接口 *.xml 文件之一有 90% 的可能存在问题。通常驻留在 res->layout->activityXXXX.xml

修复界面文件(尝试使用单个按钮或其他东西使其变得非常简单),并且 R.java 文件将由以下方式生成自动ADT。此时,您应该开始注意到主程序/活动中的 R.layout.XXXX not found 类型错误。这是一件好事,因为现在您可以创建/重命名正确的布局项并修复您的项目。

只是我的2分钱......

This is where ADT Android project is a bit different from a normal Java project. An android project requires a constantly updated persistent R.java file. Among other things, the R.java file has all the resources IDs provided in your App and maintains interface's consistency.

So first make sure R.java file is being generated for your android project.

  • To find that out first go to the top of ADT (Eclipse) and clean the project PROJECT->CLEAN
  • If the 'Build Automatically' is checked the R.java file should be created now.
  • To confirm, you can simply search the file in the project folder. You won't find it if its not being created.
  • If R.java is not being created, there is 90% chance that there is some issue with one of your interface *.xml files. Which usually resides in res->layout->activityXXXX.xml

Fix the interface file/s (try making it very simple with a single button or something) and R.java file will be generated by ADT automatically. At this point you should start noticing R.layout.XXXX not found type errors in your main program/activities. Which is a good thing, because now you can create/rename correct layout items and fix your project.

Just my 2 cents....

合久必婚 2024-10-21 17:10:21

同样的事情也发生在我身上,纠正了检查 res xml 文件中的任何错误,并将目标 1.5 更改为 2.2 android(就我而言)
当我从示例或其他开发人员导入外部代码或在线源代码时,通常会发生这种情况。

same happened to me, corrected checking any errors in res xml files, and changing target 1.5 to 2.2 android (on my case)
this usually happens to me when importing external code or online source code from examples or other developers.

空宴 2024-10-21 17:10:21

嗨,这个周末我遇到了这种情况。

发生这种情况的实际原因是我在首选项中取消选中 Android 的更新选项,因为它无法与以前版本的 Eclipse 一起使用。现在,尽管 Eclipse 似乎运行良好,但当我 PROJECT->CLEAN 项目时,R.java 丢失了。

尝试重新勾选 Android 更新选项(窗口 -> 首选项 -> 安装/更新)。

这完全解决了我的问题,我不必再麻烦地扫描我的许多 xml 文件或图像资源的每一行。

希望这对某人有帮助:)

Hi I had this happen to me this weekend.

The actual reason this had happened was that I had unchecked the update option for Android in the preferences as it wasn't working with the previous version of Eclipse. Now although the eclipse was seeming to run fine, when I PROJECT->CLEAN the project the R.java was missing.

Try re-ticking the android update option (WINDOW -> PREFERENCES -> INSTALL/UPDATE).

This completely solved my issue and I did not have to both with any hassle of scanning each line of my many xml files or my image resources.

Hope this helps someone :)

倦话 2024-10-21 17:10:21

我刚刚遇到了同样的问题。

结果我的源代码控制插入了一个Added>>>>>>>>>>>>>>>>>我的 mainifest 文件的部分。

我所要做的就是修复清单文件,然后所有错误都消失了。

I just Encountered the same problem.

Turned out my Source control had inserted an Addedd>>>>>>>>>>>>>>>> section to my mainifest file.

All I had to do was fix the manifest file and then all the errors disappeared.

何处潇湘 2024-10-21 17:10:21

我在向我的项目添加一些新资源时发生了这种情况。
每次资源更新后都会构建 R 文件。

我的错误是文件名不正确。其中一个图像资源的名称中包含大写字母,这导致构建无法创建 R 文件。更改名称很简单。

我建议当您一次添加太多资源时,只需确保它们的命名约定即可。很容易错过这样的事情。

资源约定: http://developer.android.com/guide/topics/资源/providing-resources.html

This happened to me while adding some new resources to my project.
The R file is build after every resource update.

The error in my case was incorrect file name. One of the image resources had a capital case letter in its name and that prevented the build from creating the R file. Changing the name simply worked.

I would suggest that when you add too many resources at a time, just make sure of the naming conventions for them. Its easy to miss such things.

Resource conventions: http://developer.android.com/guide/topics/resources/providing-resources.html

你的笑 2024-10-21 17:10:21

我有类似的问题。那是因为我将可绘制文件 picture.jpg 保存为 picture.png,一旦我将名称更改为以前的名称,它就开始工作。希望这对将来的人有帮助

I had the similar problem. That was because I saved a drawable file picture.jpg as picture.png, as soon as I changed the name to previous one it started working. hope this would help someone in future

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