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

I keep getting this error. Should I just make id a field?

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?

This question is related to android eclipse sdk

The answer is


It gave me enough pain but I found the solution:

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

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.


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.


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.


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.

Hope this helps.


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.


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:

<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)

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.

Hope this help.


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.

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


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.


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

<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"
/>


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.


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

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.


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


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


One possible solution:-

Summary: make sure you are using import com.yourpkgdomainname.yourpkgappname.R instead of 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.

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

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


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to sdk

I am receiving warning in Facebook Application using PHP SDK Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Difference between OpenJDK and Adoptium/AdoptOpenJDK Error:Failed to open zip file. Gradle's dependency cache may be corrupt Can't accept license agreement Android SDK Platform 24 Automatically accept all SDK licences Android SDK location should not contain whitespace, as this cause problems with NDK tools Android SDK folder taking a lot of disk space. Do we need to keep all of the System Images? "The following SDK components were not installed: sys-img-x86-addon-google_apis-google-22 and addon-google_apis-google-22" Can't find SDK folder inside Android studio path, and SDK manager not opening