Nate
Nate

Reputation: 412

Android Studio compile error with added resources

I have added this line of code to my program:

icon1.setImageResource(getResources().getIdentifier(item1, "drawable", getApplicationContext().getApplicationInfo().packageName));

And have added images (.png) to my drawable folder. When I compile I get the following error:

Error:(773, 32) error: <identifier> expected
Error:(773, 37) error: illegal start of type

The error location in R.java:

public static final class drawable {
        public static final int 1001=0x7f020000;
        public static final int 1004=0x7f020001;
        public static final int 1006=0x7f020002;
        public static final int 1011=0x7f020003;
        public static final int 1018=0x7f020004;
        public static final int 1026=0x7f020005;
        public static final int 1027=0x7f020006;
        ..........

Each one of those throws its own pair of those errors.

I'm putting the images into the /res/drawable-hdpi folder. Is the issue that the file names are all numeric (ex. 1023.png)?

What could be the cause of this?

Upvotes: 2

Views: 2182

Answers (1)

reece
reece

Reputation: 8145

The lines like:

public static final int 1001=0x7f020000;

are saying "declare a variable with type int whose name is 1001 and whose value is 0x7f020000". Names should be identifiers (consisting of the characters a-z, A-Z or _, with 0-9 being allowed only after the first character).

In this instance, 1001 is an integer not an identifier, which is why you get the " expected" error. The "illegal start of type" error is referring to the same issue (the compiler is expecting an identifier to provide the name of the type).

The items in R.java should be things like:

public static final int actionbar_logo=0x7f020000;

The drawable identifiers are created from the filenames and as you have numeric filenames it is using those values. You should give the images sensible names or append a prefix to them, e.g. r1023.

Upvotes: 10

Related Questions