简体   繁体   中英

Eclipse can't find generated ids from R.java

I have strange problems with Eclipse (Kepler) and the R.java file. The symptom is that often (but not always), Eclipse either doesn't generate the R.java file at all, or if it does generate it, it marks some of the IDs as not resolvable in my source. Even though those IDs show up in the R.java file. I've been using Project > Clean for a million times now, but this behaviour just keeps coming back. Obvious things as restarting Eclipse do not seem to change anything. Here's an example:

(GameActivity.java)
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game); //  "game" is marked as not resolvable
}

(R.java)
public final class R {
    public static final class layout {
        public static final int game=0x7f030000;
        public static final int main=0x7f030001;
    }
}

Any ideas what is going on here?

After your find and fix the wrong code, try clean all projects and generate it again.This is because you have one or more errors in your code.

Please verify the project. After your find and fix the wrong code it should work

R.java is a file that the Android Eclipse plugins creates while building your application. R.java is created under the "gen" directory. This file is generated from the information in the "res" directory. If you run select "Project" -> "Clean..." on the Eclipse menu, it will remove and then regenerate the R.java file.

The problem "R cannot be resolved" happens when you change your package name in the AndroidManifest.xml file. It uses your Android package name to create a subdirectory under the "gen" directory where it stores the R.java file.

Eclipse may have problems executing clean, because it is confused about where the R.java file is when you have changed the Android package name. You can either rename the subdirectory under gen to match your new package name, or you can change your package name back to the old name. Do the clean and then change the package name to the new name you want. This works best if you stop Eclipse from trying to build while you are changing the package name. Under the "Project" menu uncheck the option to "Build Automatically" and also when the "Clean..." dialog asks if it should "Start a build immediately" uncheck the box so it doesn't try to build while you are changing the package name. After you have changed the name you can turn "Build Automatically" back on again.

Note that if your AndroidManifest.xml file package name does not match your Java package name, Eclipse will end up automatically adding an "import .R;" line in all your .java files that have any references to R. If you change your AndroidManifest.xml package name, sometimes Eclipse does not update all of these added imports. If that happens, use the Eclipse refactoring (ALT + Shift + R) to change the import statement in one of your Java files to your new AndroidManifest.xml package name. It is best to do this while you have disabled "Build Automatically".

You can get the reference from here

Thanks for all your suggestions. I tried it all without success. Tried removing imports, clean+rebuild project, looking for errors in code/xml files (none), checked package names, restarted Eclipse many times in the process... all seemed ok.

Mysteriously after a while the error went away. I know this sounds lame, but I'm blaming this on a buggy Eclipse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM