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
Post a Comment