android - adding a RelativeLayout to a LinearLayout dynamically -
android - adding a RelativeLayout to a LinearLayout dynamically -
i trying relativelayout created dynamically (with views added dynamically it) , add together dynamic created linearlayout. follows code:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.d("tag", "on create"); linearlayout mainlo = (linearlayout) findviewbyid(r.id.linearlayout); log.d("tag", "linear layout created"); setcontentview(r.layout.activity_main); log.d("tag", "content set"); rello = new relativelayout(this); relativelayout.layoutparams lpmatchparent = new relativelayout.layoutparams( relativelayout.layoutparams.match_parent, relativelayout.layoutparams.wrap_content); lpmatchparent.addrule(relativelayout.align_parent_top); rello.setbackgroundcolor(0xbbbbbbbb); rello.setid(10); log.d("tag", "rello created"); btn1 = new button(this); btn1.settext("btn1"); btn1.setid(1); btn1.setonclicklistener(this); btn1created = true; log.d("tag", "button 1 created"); btn2 = new button(this); btn2.settext("btn2"); btn2.setid(2); btn2.setonclicklistener(this); btn2created = true; log.d("tag", "button 2 created"); btn3 = new button(this); btn3.settext("btn3"); btn3.setid(3); btn3.setonclicklistener(this); btn3created = true; log.d("tag", "button 3 created"); lpwrapcontent = new relativelayout.layoutparams( relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content); lpwrapcontent.addrule(relativelayout.center_horizontal); lpwrapcontent.setmargins(10, 2, 10, 2); log.d("tag", "lpwrapcontent created"); lpwrapcontentright = new relativelayout.layoutparams( relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content); lpwrapcontentright.addrule(relativelayout.right_of, btn1.getid()); lpwrapcontentright.setmargins(10, 2, 10, 2); log.d("tag", "lpwrapcontentright created"); lpwrapcontentleft = new relativelayout.layoutparams( relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content); lpwrapcontentleft.addrule(relativelayout.left_of, btn1.getid()); lpwrapcontentleft.setmargins(10, 2, 10, 2); log.d("tag", "lpwrapcontentleft created"); rello.addview(btn1, lpwrapcontent); rello.addview(btn2, lpwrapcontentright); rello.addview(btn3, lpwrapcontentleft); log.d("tag", "views added"); mainlo.addview(rello, lpmatchparent); log.d("tag", "rello added linear layout"); }
i null pointer exception on lastly line mainlo.addview(rello, lpmatchparent);
, thought why?
you should alter order
setcontentview(r.layout.activity_main); linearlayout mainlo = (linearlayout) findviewbyid(r.id.linearlayout);
first setcontentview(...)
, initialize layout.
android dynamic android-linearlayout relativelayout creation
Comments
Post a Comment