我对Android很陌生。我正在尝试开发一个使用表面视图的游戏应用程序。应用程序运行正常,但当我退出时,它会显示onDraw()方法上的空指针异常,然后应用程序就会崩溃。请先帮我一下,谢谢。这是我的密码:-
public class Gameloopthread extends Thread {
private Gameview view;
static final long fps=30;
boolean running;
public Gameloopthread(Gameview view)
{
this.view=view;
}
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
public void setRunning(Boolean run)
{
running=true;
}
@Override
public void run() {
long tickps=3000/fps;
long startTime=0;
long sleepTime;
while(running)
{
Canvas c=null;
try{
c=view.getHolder().lockCanvas();
//Log.d("Canvas==>",""+c);
synchronized(view.getHolder()){
view.onDraw(c);
}
}finally{
if(c!=null){
view.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime=tickps-(System.currentTimeMillis()-startTime);
try{
if(sleepTime>0)
{
sleep(sleepTime);
}else{
sleep(30);
}
}catch(Exception e){
}
}
super.run();
}
//
}发布于 2013-06-04 23:11:04
您应该看看lockCanvas调用,表面可能已经被销毁,并且您已经将一个空画布引用传递给了view.onDraw()。API中有一些关于如何处理表面状态的建议。
http://developer.android.com/reference/android/view/SurfaceHolder.html#lockCanvas()
https://stackoverflow.com/questions/16783969
复制相似问题