-1

I need to find out what is missing for a drawCircle method on surfaceview. calling lockcanvas after calling getholder() in onCreate and also tried same from the method but I got error.

here is mainactivity:

    public class MainActivity extends AppCompatActivity {

    private Main2Activity contentView;
    Tester tester;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d("App", "App launched");

        tester = new Tester(this);
//        GameView gameView = new GameView(this);
        Log.d("App", "set calling.");

        setContentView(tester);


    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("App", "onResume");

        tester.resume();
    }
}

Here the method to draw circle:

public class Tester extends SurfaceView {

Canvas canvas;
Paint paint;
boolean b;
SurfaceHolder surfaceHolder;

    public Tester(Context context) {
        super(context);
        Log.d("App", "in other context.");

//        canvas = new Canvas();
//        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//        surfaceHolder = getHolder();
//        canvas = surfaceHolder.lockCanvas();
//        setWillNotDraw(false); //this line is very important!

    }

/*
    public void run() {
        Log.d("App", "in running.");

        synchronized (canvas = surfaceHolder.lockCanvas()) {
            while (b) {

                drawing();
                Log.d("App", "drawing passed");
            }
        }
    }
*/
    private void drawing() {
        Log.d("App", "posting.");
        surfaceHolder = getHolder();
        canvas = surfaceHolder.lockCanvas();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        synchronized (canvas = surfaceHolder.lockCanvas()) {

                paint.setColor(Color.MAGENTA);
                canvas.drawCircle(100, 200, 20, paint);
                surfaceHolder.unlockCanvasAndPost(canvas);

        }
    void resume() {
        b = true;
        drawing();
    }
}

Logcat:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Canvas.drawCircle(float, float, float, android.graphics.Paint)' on a null object reference

I failed to find what can be mistaken, what is not instantiating on this simple code snippet? Thanks for your time.

Rifat
  • 1,700
  • 3
  • 20
  • 51

1 Answers1

0

As you mentioned in your comment:

please mention the reason to nullPointer exception. Im asking to know it. :)

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Canvas.drawCircle(float, float, float, android.graphics.Paint)' on a null object reference

Your LogError is clearly saying that the object of Canvas is null, which means that instance of Canvas that you using in your code did not exist or is null.

Look here:

here --> canvas.drawCircle(100, 200, 20, paint);

The reference of Canvas here canvas is null, try to init it.

W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • But i am instantiating it by calling "canvas = surfaceHolder.lockCanvas();" maybe missing something, how else can i do it? calling "new Canvas" showed no effect. All commented code in constructor was tested. Thanks. – Rifat Jan 12 '17 at 07:04