I'm trying to get an application to paint something on a canvas every half a second, but the SurfaceHolder.getSurface().isValid()
returns false, and when I call SurfaceHolder.lockCanvas()
this returns null
.
As per this SO question, I should use a SurfaceHolder.Callback.surfaceCreated
but the surface is never created.
The onCreate
method from my main Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
ChartPainter p = new ChartPainter(this);
}
And part of my ChartPainter.java
public ChartPainter(Context context) {
super(context);
holder = getHolder();
final boolean a[] = new boolean[1];
a[0] = false;
holder.addCallback(new Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
a[0] = true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
});
} [...]//more omitted code here
How do I create the surface for the SurfaceHolder
?