私はAndroidが初めてです。サーフェス ビューを使用してゲーム アプリを開発しようとしています。アプリは正常に実行されますが、終了時にonDraw()メソッドで null ポインター例外が表示され、アプリがクラッシュします。私を助けてください、そして前もって感謝します。これが私のコードです:-
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();
}
//
}