我正在编写一个库,其中surfaceview也在库本身中定义。但是当我试图调用surfaceview中定义的方法时,我得到了异常。请帮我解决问题 这是surfaceview的代码
public class AndroidGrapics extends SurfaceView implements SurfaceHolder.Callback{
public SurfaceHolder surfaceHolder = null;
public DrawingBoard(Context context) {
super(context);
//Retrieve the SurfaceHolder instance associated with this SurfaceView.
surfaceHolder = getHolder();
//Specify this class (DrawingBoard) as the class that implements the three callback methods required by SurfaceHolder.Callback.
surfaceHolder.addCallback(this);
}
//SurfaceHolder.Callback callback method.
@Override
public void surfaceCreated(SurfaceHolder holder) {
//Create and start a drawing thread whose Runnable object is defined by this class (DrawingBoard).
// new Thread(this).start();
}
public Canvas getCanvas()
{
return surfaceHolder.lockCanvas();
}
public void render(Canvas canvas,Paint paint) {
// canvas = surfaceHolder.lockCanvas();
//Fill the entire canvas' bitmap with 'black'.
canvas.drawColor(Color.BLACK);
//Instantiate a Paint object.
Paint paint1 = paint;
//Set the paint color to 'white'.
paint1.setColor(Color.WHITE);
//Draw a white circle at position (100, 100) with a radius of 50.
canvas.drawCircle(100, 100, 50, paint1);
}
//Neither of these two methods are used in this example, however, their definitions are required because SurfaceHolder.Callback was implemented.
@Override
public void surfaceChanged(SurfaceHolder sh, int f, int w, int h) {}
@Override
public void surfaceDestroyed(SurfaceHolder sh) {}
}
这是活动
public class Activity7s extends Activity implements Runnable{
DrawingBoard draw;
Paint paint=new Paint();
Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw= new DrawingBoard(this);
System.out.println("_______________________________"+draw.surfaceHolder);
//Set the content view to a new instance of DrawingBoard.
//canvas = draw.getCanvas();
System.out.println("--------------------------"+canvas);
new Thread(this).start();
setContentView(draw);
}
@Override
public void run() {
canvas = null;
while(true)
{
if(!surface.getSurface().isValid())
{
continue;
}
try {
canvas = draw.getCanvas();
System.out.println("-bbbbbbbbbbbbbbbbbbbbbbbbb-"+draw.getCanvas());
synchronized (draw.surfaceHolder) {
draw.render(draw.surfaceHolder.lockCanvas(),paint);
}
} finally {
System.out.println("--------------------------"+canvas);
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (canvas != null) {
draw.surfaceHolder.unlockCanvasAndPost(draw.surfaceHolder.lockCanvas());
}
}
}
}
}
并且logcat错误是
04-05 17:57:44.480: W/dalvikvm(3817): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
04-05 17:57:44.490:E / AndroidRuntime(3817):致命异常:Thread-153 04-05 17:57:44.490:E / AndroidRuntime(3817):java.lang.NullPointerException 04-05 17:57:44.490:E / AndroidRuntime(3817):at com.example.sexample.AndroidGrapics.render(AndroidGrapics.java:39) 04-05 17:57:44.490:E / AndroidRuntime(3817):at com.example.sexample.Activity7s.run(Activity7s.java:35) 04-05 17:57:44.490:E / AndroidRuntime(3817):at java.lang.Thread.run(Thread.java:856)
答案 0 :(得分:0)
终于找到了解决方案
我刚刚在run方法中添加了以下几行。
if(!sHolder.getSurface().isValid())
{
continue;
}
现在工作正常。
更新了问题中的代码。