起点电子阅读器:eclipse - Android Question - "Id cannot be re...

来源:百度文库 编辑:九乡新闻网 时间:2024/05/04 14:24:54

I keep getting this error... should I just make "id" a field? My code:

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?

android eclipse sdk error-message id link|edit|flag asked Jul 21 '10 at 3:18 Mia
8417
95% accept rate If you're using Eclipse, errors aren't just in the "Problems" view -- they're also attached to a particular file, and usually to a particular spot within the file. Look for the red error indicators and squiggly underlines to help figure out where the error is. – MatrixFrog Jul 21 '10 at 4:37

3 Answers

active oldest votes up vote 6 down vote accepted

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.

link|edit|flag answered Jul 21 '10 at 3:28 Andy Zhang
1,09215 up vote 4 down vote

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.

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.

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.

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

link|edit|flag answered Jul 21 '10 at 3:55 Hamy
1,3511517 That helped, but now I have another issue. When I import android.R, the id error is gone, but now the main and mainanim files have the error "cannot be resolved or are not a field". And vice-versa when I delete import android.R. I tried import android.R.xml and import android.R.id, nothing worked. What would fix this? Oh and I'm up to date. Thanks lots. :) – Mia Jul 21 '10 at 5:26 Mia, The trick here is that there are two classes, both called 'R'. You can't import both of them, because Eclipse (well, the compiler really) would not know which class you mean when you say R.. In order to fix this you should probably remove the line import android.R, and you clean the project. Then find all the places where there are still errors and change your code from R. to android.R. - Hope that helps, this stuff can be tricky – Hamy Jul 28 '10 at 19:16 up vote 2 down vote

hi

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.