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.