android - Why findViewById() throws Null Exception? -


why findviewbyid throws null exception?here layout , sourcecode file:

<relativelayout  ... tools:context=".mainactivity" >  <textview      android:id="@+id/usernametext"     android:layout_width="wrap_content"     android:layout_height="wrap_content"/>  </relativelayout> 

and here source code in mainactivity:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);           try{         textview text=(textview)mainactivity.this.findviewbyid(r.id.usernametext);         text.settext(10);     }catch(exception e){         log.i("log", e.getmessage()+"error!"); // logcat message      }  } 

however, findviewbyid()returns null, , don't know why. code simple.

text.settext(10); 

in way looking string id = 10; should change in

 text.settext(string.valueof(10)); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -