android - how to set 'this' object(TableLayout) by findViewById? -
i defined own tablelayout extents tablelayout. in class i'm trying create mytablelayout object:
int tablelayoutid = r.id.tablelayoutid; mytablelayout tablelayout = new mytablelayout(this); tablelayout.createtablelayout(tablelayoutid);
in last line above, object tablelayout calls function 'createtablelayout'. in function, i'm trying set it('this' object) findviewbyid i'm doing silly guess. how should set it?
public class mytablelayout extends tablelayout { public mytablelayout(context context) { super(context); } public void createtablelayout(int tablelayoutid) { // should write here(instead line below) in order let // 'this' have returned view findviewbyid = (mytablelayout) findviewbyid(tablelayoutid); ... } }
or maybe should other way?
====================================
=============== edit ================
====================================
after reading cricket comment, changed code in class:
if (getresources() != null) { mytablelayout tablelayout = (mytablelayout) getresources().getlayout(r.layout.tablelayout); if (tablelayout != null) { tablelayout.createtablelayout(); } }
my xml contains mytablelayout object create table programmatically through it.
and mytablelayout looks so:
public class mytablelayout extends tablelayout { public mytablelayout(context context) { super(context); } public void createtablelayout() { (int = 0; < numrows; ++i) { tablerow tablerow = new tablerow(getcontext()); tablerow.setlayoutparams(new tablerow.layoutparams(tablerow.layoutparams.fill_parent, tablerow.layoutparams.wrap_content)); (int j = 0; j < numcols; ++j) { mybutton mybutton = new mybutton(getcontext(), buttonid, buttonviewheight, buttonviewwidth, buttonviewtext); mybutton.setlayoutparams(new tablerow.layoutparams(buttonviewheight, buttonviewwidth)); tablerow.addview(mybutton); } addview(tablerow, new tablelayout.layoutparams(tablelayout.layoutparams.fill_parent, tablelayout.layoutparams.wrap_content)); } }
}
and app crashes. know why?
the breakpoint reaches line , crashes:
mytablelayout tablelayout = (mytablelayout) getresources().getlayout(r.layout.tablelayout);
succeeded finally.
1.
i removed mytablelayout xml.
2.
in class' oncreate accessed layout want add mytablelayout:
linearlayoutgame = (linearlayout) findviewbyid(r.id.linearlayout);
3.
i created , added layout
mytablelayout tablelayout = new mytablelayout(this); tablelayout.createtablelayout(); linearlayout.addview(tablelayout);
Comments
Post a Comment