问题
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?
回答1:
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 as1000
.
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".
回答2:
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.
回答3:
I know I'm late to the party but I just ran into this myself when I started working with the ActionBar component.
ScootrNova's answer led me to the solution. The problem was the Android-recommended icon pack that I downloaded for use in the tutorials. All the filenames began with integers (1_xxx.png, etc). I removed the ones I wasn't using, renamed the others to something that began with a letter (I used "icon_xxx.png" as an example), and it compiled without error.
回答4:
I had the same problem and I solved it after putting digit after text not at begining. I had line 2_Activity and I changed it to Activity2. It was my solution. Hope that it will help someone.
回答5:
I had the same problem. Resolution: Look at the res files, drawlables or inside xml and rename files or strings that start with (number underscore string) 1_string to string_1.
Hope this helps to resolve the problem.
来源:https://stackoverflow.com/questions/15037450/strange-error-in-r-java-even-after-cleaning-the-project-underscores-can-only