============问题描述============
MyGameView:
private Drawable myBackground;
public MyGameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
myContext = context;
//mBackground获取background的图片
myBackground = myContext.getResources().getDrawable(R.drawable.background);
}
private void drawBackground(Canvas canvas){
canvas.drawColor(Color.WHITE); canvas 是空值
//获取屏幕的高度。宽度
WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE);
@SuppressWarnings("deprecation")
int width = wm.getDefaultDisplay().getWidth();
@SuppressWarnings("deprecation")
int height = wm.getDefaultDisplay().getHeight();
myBackground.setBounds(0, 0, width, height);
myBackground.draw(canvas);
}
protected void Ondraw(Canvas canvas) {
// TODO Auto-generated method stub
super.Ondraw(canvas);
//用户画背景
drawBackground(canvas);canvas是空值
}
MysurfaceView:
private class DrawThread implements Runnable{
private boolean myRun = true;
@Override
public void run() {
// TODO Auto-generated method stub
while(myRun){
Canvas canvas = ourHolder.lockCanvas();
Ondraw(canvas);canvas是空值
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
三种用红色标记的canvas都是空值,这个问题该怎么解决?
============解决方案1============
Canvas canvas = ourHolder.lockCanvas(); 这个holder是怎么获取到的? 可能是这个holder没有关联上SurfaceView