Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange error in R.java, even after cleaning the project: "Underscores can only be used with source level 1.7 or greater"

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?

like image 695
Nora Powers Avatar asked Feb 23 '13 05:02

Nora Powers


2 Answers

This is a combination of two things:

  1. Java identifiers cannot start with a digit. The first character should be a letter.

  2. 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".

like image 157
Stephen C Avatar answered Nov 10 '22 02:11

Stephen C


I just now tried renaming an existing drawable in a compiling-fine Android project of mine and Eclipse threw this dialog up:

Eclipse Error (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.

like image 3
Charles Madere Avatar answered Nov 10 '22 02:11

Charles Madere