Variables cannot be resolved or is not a field in Java/Eclipse?

Go To StackoverFlow.com

1

In the main.xml i have :

 <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Your total is 0" 
        android:textSize="45dp"
        android:layout_gravity ="center"
        android:gravity = "center"
        android:id="@+id/tvDisplay"
        />
    <Button
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:text = "+"
        android:layout_gravity="center"
        android:textSize = "20dp"
        android:id="@+id/bAdd"
        />

        <Button
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:text = "-"
        android:layout_gravity="center"
        android:textSize = "20dp"
        android:id="@+id/bSub"
        />

And in the main body i have:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        counter = 0;
        add = (Button) findViewById(R.id.bAdd);
        sub = (Button) findViewById(R.id.bSub);     
display = (TextView) findViewById(R.id.tvDisplay); 

It cites the (R.id.bAdd/Sub/Display) that it cannot be resolved or is not a field. I've been following a youtube guide on how to do this exactly, so i'm confused as to how i have an error, and how i correct it.

2012-04-04 19:14
by Matt Smith


0

R.java is extremely finicky in Eclipse. If you haven't tried cleaning the project, restarting Eclipse, etc, try that. Is R.java found in your gen folder? Are other variables that should be found in R also unable to be resolved or are those the only ones? If all else fails you can try deleting R.java and cleaning the project, and assuming your XML doesn't contain errors it should re-generate.

2012-04-04 19:20
by Chris
I'm not seeing an R.java file in my gen folder, and those are the only variables i have in my project so far. I'm new at this so i'm not sure how you'd clean it since it's as basic as it can get for me - Matt Smith 2012-04-04 19:52
If gen > your.package.name > R.java is not there, then that explains why those variables are throwing errors. Click your project on the left and then go to Project > Clean, on the top of the screen/window. If R.java isn't generated than your xml has an issue or there's another underlying error in your Android project - Chris 2012-04-04 20:00
Okay, i found R.java. Tried cleaning my project, but no go - Matt Smith 2012-04-04 20:26
If R.java doesn't contain the variables that can't be resolved, then you'll have to delete it and clean. Make sure there aren't any errors or warnings in your project, especially in the xml. If it still doesn't work after a few attempts you may have to restart Eclipse and try again. Unless there's a missing piece of the puzzle here I'll bet it's Eclipse, well, being Eclipse - Chris 2012-04-04 20:32
I do have an error in my XML. android:text = "+" And the error is "Hardcoded String "+", should use @string resource." R.java contains a line about the variables i'm looking for, though, so i assume it's safe - Matt Smith 2012-04-04 20:38
Sorry I can't be of more help; without looking at the project I can only assume so much. I'd make cleaning up the XML your first priority and see if that doesn't fix things after another delete/clear - Chris 2012-04-04 20:45
I'm not sure how to fix the xml problem, either - Matt Smith 2012-04-04 21:01


0

Check to see if java.R is being imported at the top of the .java file where you're having the issues. None of the other solutions worked for me, but this did. If it is being imported, you should have a yellow warning. Remove the line, and don't import it.

2014-02-13 21:32
by FCo
Ads