2

我是安卓新手。我正在尝试使用表面视图开发游戏应用程序。该应用程序运行正常,但最后当我退出时它在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();

    }
//  

}
4

1 回答 1

0

您应该查看 lockCanvas 调用,表面可能已被破坏,并且您已将 null 画布引用传递给 view.onDraw()。API 中有一些关于如何处理表面状态的建议。

http://developer.android.com/reference/android/view/SurfaceHolder.html#lockCanvas()

于 2013-06-04T23:11:04.920 回答