Search code examples
javaandroideclipsefieldr.java-file

After R.java regeneration, many fields not recognized


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.


Solution

  • 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.