Question

I am writing a library in which surfaceview is also defined in the library itself. but when i tried to call the methods defined in surfaceview i am getting exception. Please help me with asolution this is the code for 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) {}

}

and this is the activity

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());
        }
    }

   } 

}

}

and the logcat error is

   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): FATAL EXCEPTION: 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)

Was it helpful?

Solution

finally found the solution

I'm just added following lines in the run method.

    if(!sHolder.getSurface().isValid())
       {
        continue;
            } 

now its working fine.

updated the code in the question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top