So everything was going quite nicely, until just a while ago when R.java decided to have this error after adding an icon (5_content_new.png
, to be exact).
I've tried cleaning the project and restarting eclipse, to no avail.
The problem code:
public static final class drawable {
public static final int 5_content_new=0x7f020000;
public static final int ic_launcher=0x7f020001;
...
}
The red line appears right under 5_
, and the error says:
Underscores can only be used with source level 1.7 or greater
Has anyone encountered a problem like this before?
This is a combination of two things:
Java identifiers cannot start with a digit. The first character should be a letter.
In Java 7, they introduced alternative syntaxes for integer literals; e.g. 1_000
is the same as 1000
.
So what is happening is that the compiler is parsing 5_content_new
as 5_ content_new
... which is reasonable if the source level was Java 7, and then telling you that you are not using Java 7. If you HAD been using Java 7, that compilation error would have been replaced by an error that said that an integer literal (5_
) was not legal at that point.
In short, the code contains something so "off the wall" that the compiler writer didn't anticipate it in the compiler diagnostic code.
The other point is that using ANY underscores in a variable, method, class or package name in Java is a style violation. Underscores should only be used in all-caps constant names like "MAX_VALUE".
I just now tried renaming an existing drawable in a compiling-fine Android project of mine and Eclipse threw this dialog up:
(If you can't see the image very well, the dialog box is saying The resource name must begin with a character.
The way that I produced this dialog was renaming a drawable file. The drawable's original name was button_blue_normal.9.png
, renamed it to 5_button_blue_normal.9.png
and pressed enter. Dialog popped up immediately after pressing enter.
I never knew this but apparently you'll need a letter-character, not a digit, at the very beginning of a drawable's file name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With