Calling text from XML to the manually created object in Activity's method in Android Studio -


i trying create submit , clear button loop in oncreate() method using programming instead of xml layout, , have defined label text of button string.xml , assigned it. however, application has been stopped when run it.

i have tried hide button label setting , can work useal(2 button displayed on apps without error), wondering if not possible call string label directly xml?? or missed so. pls give me comment.

    protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      tablelayout tablelayout = new tablelayout(this);     tablelayout.setorientation(tablelayout.vertical);     setcontentview(tablelayout);      button[] buttons = new button[2];      //button label     buttons[0].settext(r.string.label_calc);     buttons[1].settext(r.string.label_clear);      for(int i=0; i<2; i++) {         buttons[i] = new button(this);         tablelayout.addview(buttons[i]);     } } 

here label setting in string.xml:

<!-- button label--> <string name="label_calc">calc</string> <string name="label_clear">clear</string> 

not sure why error happen when used resource string. have tried suggestion joao below, virtual device still error message "unfortunately, application has been stopped.

error message logcat below:

04-26 12:20:27.923 992-992/com.skcalc.stockcalculator e/androidruntime﹕ fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{com.skcalc.stockcalculator/com.skcalc.stockcalculator.infoactivity}: java.lang.nullpointerexception caused by: java.lang.nullpointerexception

i fixed it, pls consider closed. problem label settled before object created. thanks

you have use

buttons[0].settext(getresources().getstring(r.string.label_calc)); buttons[1].settext(getresources().getstring(r.string.label_clear)); 

to use strings defined in resources.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -