Question

I had 2 errors that i could not resolve, so i "cleaned my project" which erased my R.java file, and did not auto-generate. after much trial and error, i found that commenting out every single error (which meant 80% of my project), then cleaning again, did work to regenerate R.java. but now instead of "R is not a field" errors, all of my variables connected to the "R.something" code in my project is now not being recognized as a field. yet i looked in R.java and they all have id #s. so i tried to refresh my project, and tried closing/opening eclipse, but nothing has worked. anyone know how to get these variables to be recognized again? thank you so much for your help.

Was it helpful?

Solution 2

Thanks everyone, each of your answers led me to fiddling around and checking/rechecking, and I discovered what was really the problem. It was not an error in the xml but a warning in my main.java file (which was causing all the red errors) about an auto-generated import that happened when I managed to regenerate my R.java file. It looked like this:

import android.R

A tiny yellow warning said that that code does not need to be there and could cause other errors. That is exactly what happened. So I deleted the import (little did I know that R.java does not need an import declaration in my main.java file). It also would not erase itself when I did [command]+[shift]+[O], so good to know that deleting it got rid of all the red errors.

Thanks for all your ideas though! Really helped.

Just to be more clear, the errors were showing up in all my R.layout.variable_name_here code, which made sense, since it was connected to R.java somehow.

Here is a sample (where I found the errors):

private void checkAnswer(boolean userPressedTrue) {
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();

        int messageResId = 0;

        if (userPressedTrue == answerIsTrue) {
            messageResId = R.string.correct_toast;
        } 
        else {
            messageResId = R.string.incorrect_toast;
        }

        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();

Both of the variables (and many others) connected to R had the errors: correct_toast and incorrect_toast.

OTHER TIPS

AFAIK R.java fails to generate due to following reasons. 1)Resources naming in upper case. 2)Spaces in Ids of layout components like button ids or so. 3)Any numbers used in naming resources.

You will also get this error if you mix dot(.) and underscore(_). For example android:id="@+id/comment_label" and android:id="@+id/comment.label" generated two field with the same name i.e comment_label hence the error.

Hope this helps someone.

you have an error in some of your xml files - layout, drawable and anything else that can be accessed via the R class. It could be just wrong syntax, but this is the most common issue for not generating the R.java.

You must find that error and fix it. Check, double check and triple check your xml files.

Make sure your layout files are having without errors and also check this one in your activity/fragment

import com.org.packagename.R;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top