0

您好我试图让我的位图淡入淡出。位图被绘制到表面视图上的画布上。我还使用精灵类将精灵绘制到我的画布上:

public class sprite {

static int x, y;
static int xSpeed, ySpeed;
static int height, width;
static Bitmap b;
static CanvasView canView;
static Paint fadePaint;


public sprite(CanvasView canvasView, Bitmap xSpriteSheet) {
    // TODO Auto-generated constructor stub

    b = xSpriteSheet;
    canView = canvasView;
    fadePaint.setAlpha(100);  //Heres where logcat gives me an error

     // divide by 2 for rows in sprite sheet
    //4 colums
    height = b.getHeight();
    width = b.getWidth();
    x = 0; y = 0;
    xSpeed = 5;
    ySpeed = 0;
}

public static void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    Rect src = new Rect(0, 0 , width, height);
    Rect dst = new Rect(x, y, x+width, y+height);
    update();

    canvas.drawBitmap(b, src, dst, fadePaint);
}
private static void update() {
    // TODO Auto-generated method stub  
    x += xSpeed;
}

这是我的 logcat 给我的:

 FATAL EXCEPTION: Thread-273
java.lang.NullPointerException
at com.mrsai.xsos.sprite.<init>(sprite.java:25)
at com.mrsai.xsos.Game$CanvasView.run(Game.java:101)
at java.lang.Thread.run(Thread.java:856)
4

1 回答 1

1

这是因为你没有初始化Paint. 改变这个:

static Paint fadePaint;

对此:

static Paint fadePaint = new Paint();
于 2013-08-03T18:47:35.287 回答