开发者

android Image problem

开发者 https://www.devze.com 2023-01-08 22:26 出处:网络
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

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.


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));


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消