首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >drawBitmap nullpointerexception

drawBitmap nullpointerexception
EN

Stack Overflow用户
提问于 2013-05-12 15:57:42
回答 2查看 1.2K关注 0票数 0

这是我的班级。我想在我的简单游戏的GameEngine中使用它。但是ı不理解这里的问题..它不工作..。

代码语言:javascript
复制
public class Droid {

    private Bitmap      bitmap; 
    private int     x;      
    private int     y;      
    private boolean         touched;    
    private Speed       speed;
    private Paint       paint;

    public Droid(Resources resources, Bitmap bitmap, int x, int y)
        {
            this.bitmap = bitmap;
            this.x = x;
            this.y = y;

            // create droid and load bitmap
            bitmap = BitmapFactory.decodeResource(resources,
                    R.drawable.droid_1);

        }

              public void draw(Canvas canvas)
        {

            canvas.drawBitmap(bitmap, x - bitmap.getWidth() / 2,
                    y- bitmap.getHeight() / 2, paint);
        }



     }

当方法运行代码时,会看到nullpointerexception at draw()ıı...ı如何解决这个问题?谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-14 01:23:47

//ı希望,ı解决了这个问题..终于..。:)

公共类Droid {

代码语言:javascript
复制
private Bitmap      bitmap; 
private int     x;      
private int     y;      
private boolean         touched;    
private Speed       speed;
private Paint       paint;

public Droid(Resources resources, Bitmap bitmap, int x, int y)
    {
        paint= new Paint();
//    this.bitmap = bitmap;   // delete this part, it will work..  :)))
    this.x = x;
    this.y = y;

        // create droid and load bitmap
        bitmap = BitmapFactory.decodeResource(resources,
                R.drawable.droid_1);

    }

          public void draw(Canvas canvas)
    {

        canvas.drawBitmap(bitmap, x - bitmap.getWidth() / 2,
                y- bitmap.getHeight() / 2, paint);
    }



 }
票数 0
EN

Stack Overflow用户

发布于 2013-05-12 15:59:58

MainActivity

代码语言:javascript
复制
    public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    MyView mv = new MyView(this,bmp,100,100); 
            // pass the bitmap and x and y co-ordinates to the constructor of Myview
    setContentView(mv); // set the content to your activity
}
  }

自定义视图类

代码语言:javascript
复制
  public class MyView extends View{ // should extend view
 Context c;
 private Bitmap   bitmap; 
 private int     x;      
 private int     y; 
 private Paint       paint;
 public MyView(Context context,Bitmap bmp, int i, int j) {
    super(context);
    // TODO Auto-generated constructor stub
    this.c=context;
    this.bitmap=bmp;
    this.x=i;
    this.y=j;
    paint= new Paint();
}
@Override
    public void draw(Canvas canvas) // override view on draw and draw the bitmap
    {

    canvas.drawBitmap(bitmap, x - bitmap.getWidth() / 2,
             y- bitmap.getHeight() / 2, paint); // instead of paint you can have null
    }
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16505312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档