vivek_Android
vivek_Android

Reputation: 1697

android Image problem

Actually I have 52 images which r basically cards. images names are from 1 to 52.

when I put all the 52 images in my drawable folder then it is showing an error in R.java file which is:

Syntax error on token "image name(any between between 1 to 52)", invalid VariableDeclaratorId

what is the problem?


thanks for replying.

i think u didnt get my problem.

i have given name to my cards from 1 to 52 because i need to randomly select one card from it.

Upvotes: 3

Views: 920

Answers (3)

Pontus Gagge
Pontus Gagge

Reputation: 17258

Resource names have to be proper Java identifiers. Call them card1 through card52 instead of just their numbers (if I understand you correctly).

EDITED TO ADD: To map an integer to the correct image, your code should manage the mapping itself. One (not terribly elegant) way is to explicitly create a Bitmap[] cardImages = new Bitmap[52]; array and assigning each resource into the array, as in e.g.

Resources r = context.getResources();
cardImages[0] = loadBitmap(r.getDrawable(R.drawable.card1));
// ...
cardImages[12] = loadBitmap(r.getDrawable(R.drawable.card13));
// ...    
cardImages[51] = loadBitmap(r.getDrawable(R.drawable.card52));

Upvotes: 1

Janusz
Janusz

Reputation: 189474

Pontus Gagge is right. Android will take the name of the everything in the drawable folder and will try to generate an R file that contains an int for every image that you are using in your app. The ints are named after the file names of your drawables. You can then later use this ints as ids to load the images from your app.

The problem is that Java does not allow a vairable name to start with a number. Your images start with a number therefore your variables in the R file will start with a number. You have to choose another name for your images.

Upvotes: 1

anon
anon

Reputation:

The problem is that Android doesn't allow to use spaces in a file identifier

Upvotes: 1

Related Questions