Android “id 无法解析或不是字段”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3296047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:33:16  来源:igfitidea点击:

"id cannot be resolved or is not a field" error?

androideclipsesdk

提问by Mia

I keep getting this error. Should I just make ida field?

我不断收到此错误。我应该只做id一个领域吗?

My code is:

我的代码是:

public void onCreate(Bundle icicle) 
{
    super.onCreate(icicle);
    setContentView(R.layout.main);
    ImageView mainimage = (ImageView) findViewById(R.id.mainanim);
    mainimage.setBackgroundResource(R.anim.mainanim);
    mainanimation = (AnimationDrawable) mainimage.getBackground();
}

How exactly do you fix this?

你是如何解决这个问题的?

回答by Hamy

Look at your importstatements at the top. If you are saying import android.R, then there that is a problem. It might not be the only one as these 'R' errors can be tricky, but it would definitely definitely at least part of the problem.

看看你import在顶部的陈述。如果你是说import android.R,那就有问题了。它可能不是唯一的,因为这些“R”错误可能很棘手,但它肯定至少是问题的一部分。

If that doesn't fix it, make sure your eclipse plugin(ADT) and your android SDK are fully up to date, remove the project from the emulator/phone by manually deleting it from the OS, and clean the project (Launch Eclipse->Project->Clean...). Sounds silly to make sure your stuff is fully up to date, but the earlier versions of the ADT and SDK has a lot of annoying bugs related to the R files that have since been cleared up.

如果这不能解决它,请确保您的 eclipse 插件 (ADT) 和您的 android SDK 完全是最新的,通过从操作系统中手动删除它来从模拟器/手机中删除该项目,并清理该项目(启动 Eclipse- >项目->清理...)。确保您的东西完全最新听起来很愚蠢,但是早期版本的 ADT 和 SDK 有很多与 R 文件相关的烦人的错误,这些错误已经被清除了。

Just FYI, the stuff that shows up in the R class is generated from the stuff in your project res (aka resources) folder. The R class allows you to reference a resource (such as an image or a string) without having to do file operations all over the place. It does other stuff too, but that's for another answer. Android OS uses a similar scheme - it has a resources folder and the class android.R is the way to access stuff in the android resources folder. The problem arises when in a single class you are using both your own resources, and standard android resources. Normally you can say importat the top, and then reference a class just using the last bit of the name (for example, import java.util.Listallows you to just write Listin your class and the compiler knows you mean java.util.List). When you need to use two classes that are named the same thing, as is the case with the auto-generated R class, then you can import one of them and you have to fully qualify the other one whenever you want to mean it. Typically I import the R file for my project, and then just say android.R.whatever when I want an android resource.

仅供参考,R 类中显示的内容是从项目 res(又名资​​源)文件夹中的内容生成的。R 类允许您引用资源(例如图像或字符串),而无需到处执行文件操作。它也做其他事情,但这是另一个答案。Android 操作系统使用类似的方案 - 它有一个资源文件夹,类 android.R 是访问 android 资源文件夹中内容的方式。当您在一个类中同时使用自己的资源和标准的 android 资源时,就会出现问题。通常你可以说import在顶部,然后只使用名称的最后一位来引用一个类(例如,import java.util.List允许你只List在你的类中写入并且编译器知道你的意思java.util.List)。当您需要使用两个命名相同的类时,就像自动生成的 R 类的情况一样,您可以导入其中一个类,并且您必须在需要时完全限定另一个类。通常我会为我的项目导入 R 文件,然后当我想要一个 android 资源时只说 android.R.whatever。

Also, to reiterate Andy, don't modify the R file automatically. That's not how it's meant to be used.

另外,重申一下 Andy,不要自动修改 R 文件。这不是它的用途。

回答by Andy Zhang

Do not modify the R class. The error means there's something syntactically wrong with your XML layouts and R cannot be auto-generated. Try looking there and post the xml code you're not sure about, if any.

不要修改 R 类。该错误意味着您的 XML 布局存在语法错误,并且 R 无法自动生成。尝试查看并发布您不确定的 xml 代码(如果有)。

Edit : also: remove "import android.R" from imports at top of file (if there)

编辑:也:从文件顶部的导入中删除“import android.R”(如果有)

回答by Doc

One possible solution:-

一种可能的解决方案:-

Summary:make sure you are using import com.yourpkgdomainname.yourpkgappname.Rinstead of import android.R

总结:确保你使用的是import com.yourpkgdomainname.yourpkgappname.R而不是import android.R

Details:The problem occured when I changed ID of a label which was being referred in other places in the layout XML file. Due to this error, the R file stopped generating at first. Eclipse is bad in handling errors with the layout files.

详细信息:当我更改在布局 XML 文件中的其他地方引用的标签 ID 时出现问题。由于此错误,R 文件首先停止生成。Eclipse 在处理布局文件错误方面很糟糕。

When I corrected the ID reference (with project clean few times and Eclipse restarts, I noticed that my import packages now has:
import android.R

当我更正 ID 引用时(项目清理了几次并且 Eclipse 重新启动,我注意到我的导入包现在有:
import android.R

Changing it to following fixed the error:
import com.example.app.R

将其更改为以下修复了错误:
import com.example.app.R

回答by Saraceni

I just fixed my problem right-clicking in the layout folder and clicking in the option Validate. Some windows will appear, you just clik OK and ist fine.

我刚刚解决了我的问题,在布局文件夹中单击鼠标右键,然后单击选项验证。将出现一些窗口,您只需单击“确定”即可。

回答by arun

May be you created a new xml file in Layout Directory that file name containing a Capital Letter which is not allowed in xml file under Layout Directory.

可能是您在布局目录中创建了一个新的 xml 文件,该文件名包含一个大写字母,这在布局目录下的 xml 文件中是不允许的。

Hope this help.

希望这有帮助。

回答by Jake

Just throwing this out there, but try retyping things manually. There's a chance that your quotation marks are the "wrong" ones as there's a similar unicode character which looks similar but is NOT a quotation mark.

只是把它扔在那里,但尝试手动重新输入。您的引号有可能是“错误”的,因为有一个类似的 unicode 字符看起来相似但不是引号。

If you copy/pasted the code snippits off a website, that might be your problem.

如果您从网站上复制/粘贴代码片段,那可能是您的问题。

回答by Ashwin

As Jake has mentioned, the problem might be because of copy/paste code. Check the main.xml under res/layout. If there is no id field in that then you have a problem. A typical example would be as below

正如 Jake 所提到的,问题可能出在复制/粘贴代码上。检查 res/layout 下的 main.xml。如果其中没有 id 字段,那么您就有问题。一个典型的例子如下

<com.androidplot.xy.XYPlot
android:id="@+id/mySimpleXYPlot"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10px"
android:layout_marginLeft="20px"
android:layout_marginRight="20px"
title="A Simple Example"
/>

回答by Senthil

Some times eclipse may confuse with other projects in the same directory.

有时 eclipse 可能会与同一目录中的其他项目混淆。

Just change package name (don't forget to change in Android manifest file also), ensure the package name is not used already in the directory. It may work.

只需更改包名称(也不要忘记在 Android 清单文件中更改),确保包名称尚未在目录中使用。它可能会起作用。

回答by kiran boghra

Just Clean your project so R will be generated automatically. This worked for me.

只需清理您的项目,即可自动生成 R。这对我有用。

回答by Don

Just came across this myself.

刚刚自己遇到了这个。

Finally found my issue was with a .png file that I added that had a capital letter in it an caused exactly the same problem. Eclipse never flagged the file until I closed it and opened Eclipse back up.

最后发现我的问题出在我添加的 .png 文件中,该文件中有一个大写字母,导致了完全相同的问题。Eclipse 从不标记文件,直到我关闭它并打开 Eclipse 备份。