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

时间:2021-08-19 00:38:52

I keep getting this error. Should I just make id a 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?

你如何解决这个问题?

17 个解决方案

#1


43  

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)

编辑:同时:删除“导入android”。从文件顶部的导入(如果有)

#2


50  

Look at your import statements 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.

查看顶部的导入语句。如果你说导入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 ->项目-> clean…)。确保您的东西是完全最新的听起来很傻,但是ADT和SDK的早期版本有很多烦人的bug,这些bug与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 import at the top, and then reference a class just using the last bit of the name (for example, import java.util.List allows you to just write List in 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 OS使用了类似的方案——它有一个资源文件夹和类Android。R是访问android资源文件夹的方法。当在单个类中同时使用自己的资源和标准的android资源时,就会出现问题。通常,您可以在顶部输入import,然后仅使用名称的最后一点引用类(例如,import java.util)。List允许您在类中编写List,编译器知道您的意思是java.util.List)。当您需要使用两个名称相同的类时,就像自动生成R类的情况一样,那么您可以导入其中一个类,并且您必须在需要时对另一个类进行完全限定。通常我为我的项目导入R文件,然后只输入android.R。当我需要android资源的时候。

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

再次重申,安迪,不要自动修改R文件。这不是它应该被使用的方式。

#3


29  

One possible solution:-

一个可能的解决方案:-

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

摘要:确保您使用的是import com.yourpkgdomainname.yourpkgappname。代替进口android

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

#4


6  

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.

我刚刚修复了我的问题,右键单击布局文件夹,然后单击选项Validate。有些窗口会出现,你只要点击OK就可以了。

#5


5  

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.

希望这个有帮助。

#6


3  

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.

如果您复制/粘贴了一个网站上的代码剪切,这可能是您的问题。

#7


3  

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所提到的,问题可能是由于复制/粘贴代码。检查主。xml在res /布局。如果没有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"
/>

#8


3  

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 manifest文件中也进行更改),确保包名不在目录中使用。它可能工作。

#9


2  

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。

#10


2  

What seems to be the problem, I just fixed mine in case anyone was wondering - Due to other errors i turned off build automatically, when i created a new project it said R.layout.main had an issue and needed to import R; So naturally as a novice, i did. Then i built manually and it had a problem with main. Try building your program as is, remove import R and it should be fine.

问题似乎是这样的,我刚刚修正了我的错误,以防有人怀疑——由于其他错误,我自动关闭了build,当我创建了一个新的项目,它说R.layout。main有问题需要导入R;作为一个新手,我很自然地做到了。然后我手工构建,它与main有问题。尝试按原样构建程序,删除导入R,应该没问题。

#11


2  

It gave me enough pain but I found the solution:

它给了我足够的痛苦,但我找到了解决办法:

PROJECT ---> Clean ----> (Sele)

#12


2  

In main.xml (or wherever your item is defined) make sure that the ID for the R item is defined with @+id/... Here is an example with a button:

在主。xml(或任何定义了项目的地方)确保R项的ID是用@+ ID /…这里有一个按钮的例子:

<Button android:text="B1" android:id="@+id/button_one"
        android:layout_gravity="center_horizontal|center"
        android:layout_height="fill_parent" android:layout_width="wrap_content" />

Each of these is important because:

每一个都很重要,因为:

  • @ must precede the string
  • @必须在字符串之前
  • + indicates it will create if not existing (whatever your item is)
  • +表示如果不存在,它将创建(无论您的项目是什么)

#13


2  

I also had this error when I was working in a Java class once. My problem was simply that my xml file, with the references in it, was not saved. If you have both the xml file and java class open in tabs, check to make sure the xml file name in the tab doesn't have a * by it.

当我在Java类中工作时,也有这个错误。我的问题只是我的xml文件(其中包含引用)没有保存。如果您在选项卡中打开了xml文件和java类,请检查以确保选项卡中的xml文件名没有* *。

Hope this helps.

希望这个有帮助。

#14


2  

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

只要清理你的项目,R就会自动生成。这为我工作。

#15


1  

I had this problem but in my case it solved by restarting the eclipse.

我有这个问题,但在我的情况下,它通过重新启动eclipse来解决。

#16


0  

For me the cause of the problem was that I had 2 images with the same name in my drawable-hdpi folder. Once I resolved that, then the R.java rebuilt itself and the problem was automatically solved.

对我来说,问题的原因是在我的draw- hdpi文件夹中有两个名称相同的图像。一旦我解决了这个问题,R。java重新构建了自己,问题自动得到解决。

#17


0  

select Project tab and click Build automatically so Build all option will be activated and then click on build all.and always start xml file name with lowercase.

选择Project选项卡并自动单击Build all选项卡,这样将激活Build all选项卡,然后单击Build all。并且总是以小写开头的xml文件名。

#1


43  

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)

编辑:同时:删除“导入android”。从文件顶部的导入(如果有)

#2


50  

Look at your import statements 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.

查看顶部的导入语句。如果你说导入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 ->项目-> clean…)。确保您的东西是完全最新的听起来很傻,但是ADT和SDK的早期版本有很多烦人的bug,这些bug与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 import at the top, and then reference a class just using the last bit of the name (for example, import java.util.List allows you to just write List in 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 OS使用了类似的方案——它有一个资源文件夹和类Android。R是访问android资源文件夹的方法。当在单个类中同时使用自己的资源和标准的android资源时,就会出现问题。通常,您可以在顶部输入import,然后仅使用名称的最后一点引用类(例如,import java.util)。List允许您在类中编写List,编译器知道您的意思是java.util.List)。当您需要使用两个名称相同的类时,就像自动生成R类的情况一样,那么您可以导入其中一个类,并且您必须在需要时对另一个类进行完全限定。通常我为我的项目导入R文件,然后只输入android.R。当我需要android资源的时候。

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

再次重申,安迪,不要自动修改R文件。这不是它应该被使用的方式。

#3


29  

One possible solution:-

一个可能的解决方案:-

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

摘要:确保您使用的是import com.yourpkgdomainname.yourpkgappname。代替进口android

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

#4


6  

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.

我刚刚修复了我的问题,右键单击布局文件夹,然后单击选项Validate。有些窗口会出现,你只要点击OK就可以了。

#5


5  

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.

希望这个有帮助。

#6


3  

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.

如果您复制/粘贴了一个网站上的代码剪切,这可能是您的问题。

#7


3  

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所提到的,问题可能是由于复制/粘贴代码。检查主。xml在res /布局。如果没有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"
/>

#8


3  

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 manifest文件中也进行更改),确保包名不在目录中使用。它可能工作。

#9


2  

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。

#10


2  

What seems to be the problem, I just fixed mine in case anyone was wondering - Due to other errors i turned off build automatically, when i created a new project it said R.layout.main had an issue and needed to import R; So naturally as a novice, i did. Then i built manually and it had a problem with main. Try building your program as is, remove import R and it should be fine.

问题似乎是这样的,我刚刚修正了我的错误,以防有人怀疑——由于其他错误,我自动关闭了build,当我创建了一个新的项目,它说R.layout。main有问题需要导入R;作为一个新手,我很自然地做到了。然后我手工构建,它与main有问题。尝试按原样构建程序,删除导入R,应该没问题。

#11


2  

It gave me enough pain but I found the solution:

它给了我足够的痛苦,但我找到了解决办法:

PROJECT ---> Clean ----> (Sele)

#12


2  

In main.xml (or wherever your item is defined) make sure that the ID for the R item is defined with @+id/... Here is an example with a button:

在主。xml(或任何定义了项目的地方)确保R项的ID是用@+ ID /…这里有一个按钮的例子:

<Button android:text="B1" android:id="@+id/button_one"
        android:layout_gravity="center_horizontal|center"
        android:layout_height="fill_parent" android:layout_width="wrap_content" />

Each of these is important because:

每一个都很重要,因为:

  • @ must precede the string
  • @必须在字符串之前
  • + indicates it will create if not existing (whatever your item is)
  • +表示如果不存在,它将创建(无论您的项目是什么)

#13


2  

I also had this error when I was working in a Java class once. My problem was simply that my xml file, with the references in it, was not saved. If you have both the xml file and java class open in tabs, check to make sure the xml file name in the tab doesn't have a * by it.

当我在Java类中工作时,也有这个错误。我的问题只是我的xml文件(其中包含引用)没有保存。如果您在选项卡中打开了xml文件和java类,请检查以确保选项卡中的xml文件名没有* *。

Hope this helps.

希望这个有帮助。

#14


2  

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

只要清理你的项目,R就会自动生成。这为我工作。

#15


1  

I had this problem but in my case it solved by restarting the eclipse.

我有这个问题,但在我的情况下,它通过重新启动eclipse来解决。

#16


0  

For me the cause of the problem was that I had 2 images with the same name in my drawable-hdpi folder. Once I resolved that, then the R.java rebuilt itself and the problem was automatically solved.

对我来说,问题的原因是在我的draw- hdpi文件夹中有两个名称相同的图像。一旦我解决了这个问题,R。java重新构建了自己,问题自动得到解决。

#17


0  

select Project tab and click Build automatically so Build all option will be activated and then click on build all.and always start xml file name with lowercase.

选择Project选项卡并自动单击Build all选项卡,这样将激活Build all选项卡,然后单击Build all。并且总是以小写开头的xml文件名。