android - getting string from resource strings.xml -


when string strings.xml , don't error right away, can't run program :

string mybuttontext = getstring(r.string.buttontext); 

(when remove line , variable program works perfectly)

i'll put program, in case you'd need it:

public class mainactivity extends appcompatactivity {      private button button;     string mybuttontext = getstring(r.string.buttontext);      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          button = (button) findviewbyid(r.id.button);          button.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {             if(mybuttontext.equals("red")){//the button's text "red"                button.settext("blue");            }             }//i'm changing text on button "blue" when says"red"         });     } } 

you should call getstring(...) within activity lifecycle ensure components under hood initialized.

in example, should call mybuttontext = getstring(r.string.buttontext); in oncreate(...)


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -