Monday, 9 September 2013

NullPointerException in SurfaceView

NullPointerException in SurfaceView

please someone should help me out i am running surface view for the first
time and cant figure out what is wrong with my code.i want it to display a
circle moving horizontally but when i run my main activity,it gives an
error.. "a java.lang.nullPointerException" ...this is the code for the
surface View class:
public class MovingBall extends SurfaceView implements
SurfaceHolder.Callback,Runnable{
SurfaceHolder holder;
Paint thePaint;
int x=0;
public MovingBall(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
holder.addCallback(this);
thePaint.setColor(Color.BLUE);
thePaint.setAntiAlias(true);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
Thread th= new Thread(this);
th.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
Canvas c=null;
try{
holder.lockCanvas();
synchronized(holder) {
draw(c);
}
}finally
{
if(c!=null)
{
holder.unlockCanvasAndPost(c);
}
}
}
}
@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
super.draw(canvas);
canvas.drawCircle(x, 40, 30, thePaint);
x++;
invalidate();
}
}
and this is the mainActivity code:
public class ShowBallActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MovingBall theBall = new MovingBall(this);
setContentView(theBall);
}
}

No comments:

Post a Comment