开发者

Android : NullPointerException while adding Resources

开发者 https://www.devze.com 2023-03-22 18:15 出处:网络
I\'m getting a NullPointerException when running this bit of code abstract class thing extends Drawable(){

I'm getting a NullPointerException when running this bit of code

abstract class thing extends Drawable(){
    Bitmap sprite;
    int spriteResource;

    public thing(){
          setResources();
          sprite=Bitmap.createBitmap(sprite,src.left,src.top,(src.right-src.left),(src.bottom-src.top),m,true);
    }

    @Override
    public void draw(Canvas c){
    bit= Bitmap.createBitmap(sprite,0,0,45,  45);// Generates the exception
    c.drawBitmap(bit, x, y, null);
    }

     abstract void setResource();
}

class otherThing extends thing(){
     @Override
      public void setResource(){
      spriteResource=R.drawable.otherThing_sprite;
      }开发者_C百科
}

Basically i'm trying to load different sprites into different classes by using the method of the parent class. But the spriteResource doesn't get set and I can't understand why? I set up the log which returned the Resource as 0. Any ideas why this is happening or how to resolve it???

Thanks


When createBitmap in thing() is trying to give the sprite variable a value, sprite is one of its in-parameters, this means that you are trying to use sprite before it is created and you get a null pointer exception.

0

精彩评论

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