Issue
I'm coding a card game for Android, so I have 40 card images, in 4 different densities, distributed into res/drawable-ldpi, hdpi, mdpi and xhdpi.
I have defined my Card class with a Rect region among its members, where I'm going to print a different image for each card. Code is:
package Maze;
import android.R;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
public class Card {
private CardNumber number;
private CardSuit suit;
private Rect region;
private boolean visible;
private Bitmap bitmap;
private Bitmap backsideImg;
public Card(CardNumber number, CardSuit suit){
this.number = number;
this.suit = suit;
this.visible = false;
region = new Rect();
setBitmap();
}
private void setBitmap(){
backsideImg = BitmapFactory.decodeFile(R.drawable.reverse);
if(suit==CardSuit.HEARTS){
switch(number){
case ONE:
bitmap = BitmapFactory.decodeFile(R.drawable.onehearts); // For onehearts.png file
break;
case TWO:
bitmap = BitmapFactory.decodeFile(R.drawable.twohearts); // For twohearts.png file
break;
case THREE:
bitmap = BitmapFactory.decodeFile(R.drawable.threehearts);
break;
case CUATRO:
bitmap = BitmapFactory.decodeFile(R.drawable.fourhearts);
break;
// And so on...
}
}
}
public void draw(Canvas c, int x, int y){
region.left = x;
region.top = y;
region.right = x + 80;
region.bottom = y + 115;
if(visible) c.drawBitmap(bitmap, x, y, null);
else c.drawBitmap(backsideImg, x, y, null);
}
}
But it is not finding the resources. It says for example "1hearts cannot be resolved or is not a field". I guess I'm missing some step, I just dragged and dropped the images to the resource folders.
What am I missing?
Solution
Rename your file names to start with a letter. The autogenerated R.drawable
fields are named the same as your file names, but have to be valid Java identifiers.
Edit:
Also, check that you are including the right R
class. You might want to update your question with the full content of the Card.java
, not just the class code.
Edit 2:
As I suspected, you are importing android.R
, which is the system resources class. You should be using the R
class generated from your resources instead. Given your package name, I'd expect it to be Maze.R
.
Answered By - Franci Penov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.