Skip to content Skip to sidebar Skip to footer

Eclipse Error With Android: Id Cannot Be Resolved Or Is Not A Field

I just started playing around with android development, and already with just an attempt at making a button, I have encountered a problem. The error I'm given in the following code

Solution 1:

I've been wasting a lot of time (two weeks) because of the same problem until I discovered the problem wasn't mine but Eclipse's. I guess there's a lot of people with the same problem.

Just try this: Save your project, close Eclipse and then open it again. So simple.

Solution 2:

Do I need to manually reference every single object I make in the layout xml file

Yes, otherwise you won't be able to do anything with those views. It's not that bad actually. So, each time you create a view in your XML, and you want to reference it, put an ID:

<View
    android:id="@+id/the_id"/>

And then, from your code you can reference it using the R class. You can type, in the example, R.id.the_id and then Ctrl+Shift+O to make Eclipse auto import the needed files.

You can speed up your productivity by using frameworks like Roboguice; I think it's for lazy people, though.

Solution 3:

This answer is not applicable to this question (looking at code you have provided). Just adding it if someone else stumbles here and above mentioned answers do not help.

If cleaning (Project --> clean) doesn't helps or saving and restarting eclipse doesn't help either, check for the following incorrect import.

import android.R;

Which Eclipse sometimes add by mistake on auto-import (Ctrl+Shift+O). Remove that line (import) and it's done :D

Solution 4:

Following this EXCELLENT tutorial , I encountered the same problem. After reading Carmello's answer (Sept 17, 2011. 07:23) I simply clicked File->Save All, and voila, 'button0' was automagically defined, and even syntax highlighted.

Solution 5:

If "R.id.button1" is not defined, then you'll get a compile error, just as you saw. If you don't define this in the layout, then it won't be defined.

You don't have to specify every object you create in the layout, but you do if you try to reference it from "R.*". You can manually create buttons and other objects that are not specified in the layout.

Post a Comment for "Eclipse Error With Android: Id Cannot Be Resolved Or Is Not A Field"