Is important to use the setContentView(R.layout.name.xml) in android -


is important use setcontentview(r.layout.name.xml) in android? in following code, want draw line in relativelayout object without using setcontentview(r.layout.name.xml). can compile successfully. stops execution time.

draw.java

 class draw extends view { paint paint;      public draw(context context) {         super(context);         paint = new paint();     }  @override     protected  void ondraw(canvas canvas) { paint.setcolor(color.red);   canvas.drawline(10,10,100,100,paint); super.ondraw(canvas); }} 

mainactivity.java

class mainactivity extends activity {     draw draw;     @override     public void oncreate(bundle s){         super.oncreate(s);         relativelayout relativelayout = new relativelayout(this);         draw = new draw(this);        relativelayout.addview(draw,200,200);      } } 

setcontentview() in android must called in oncreate() method before getting references views in activity.

without setcontentview() activity won't inflated or assigned layout.

as result app won't start because start activity has no layout or ui display.

so, include setcontentview(), passing layout parameter, in oncreate() method of activity.

hope helps.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -