具有画布的表面视图类中的线程不断使程序崩溃,因为该线程遇到了致命异常。然后 NullPointerException 来了。它说问题发生在第 28 行,但一切都很好。然后程序跳过了数百帧。它说应用程序在主线程中做了太多的工作。有没有什么办法解决这一问题?
这是代码:
package com.mtprogramming.blockfight;
import android.content.Context;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SView extends SurfaceView implements Runnable{
SurfaceHolder sviewholder;
Thread thread = null;
Boolean game = true;
public SView(Context context) {
// TODO Auto-generated constructor stub
super(context);
sviewholder = getHolder();
thread = new Thread(this);
thread.start();
}
@Override
public void run() {
while(game){
if(sviewholder.getSurface().isValid()){
continue;
}
Canvas sview = sviewholder.lockCanvas();
sview.drawRGB(33, 66, 150);
sviewholder.unlockCanvasAndPost(sview);
}
}
}