0

I am a beginner in programming... so maybe this question sounds funny to you. I have a problem on creating a new CanvasView inside the MainActivity because it has something written inside its brackets. Here is my MainActivity:

public class MainActivity extends AppCompatActivity {
    CanvasView myView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //the line below doesnt work
        myView = new CanvasView();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void button1(View v){
        CanvasView.l = 400;
        myView.invalidate();
    }
}

and here is my CanvasView:

public class CanvasView extends View{
    Paint paint = new Paint();
    static int l = 500;

    public CanvasView (Context context, AttributeSet attrs) {
        super(context);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(10);
    }
    @Override
    public void onDraw(final Canvas canvas) {
        canvas.drawLine(100,100,l,100,paint);
    }
}
Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
nok4
  • 1
  • 1

1 Answers1

0

Create yout canvas after super.onCreate and setContentView like below

super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myView = new CanvasView();

an_droid_dev
  • 1,136
  • 14
  • 18
  • I dont really understand what you mean with your first line: "super.onCreate method. " – nok4 Feb 18 '17 at 10:42