java - Changing text of a TextView throws NullPointer Exception -
i'm following basic tutorial on android website. however, i'm trying change few things fun. instance, instead of creating runtime textview
(as in tutorial) within method oncreate(...)
of displaymessageactivty
class, i've defined static textview
in xml layout file because i'd show other things (e.g., static message resulting entered message main screen). so, in corresponding xml file of displaymessageactivty
i've defined textview
, other stuff as:
[...] <textview android:id="@+id/dynamic_msg" android:layout_width="0dp" android:layout_height="wrap_content" android:text="ciao" android:textsize="40dp" android:layout_below="@id/static_msg" android:layout_alignparentleft="true" android:layout_toleftof="@+id/times" /> [...]
then, access textview
in following way (oncreate(...)
method of displaymessageactivty
class):
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // message intent intent intent = getintent(); string message = intent.getstringextra(mainactivity.extra_message); // here want change content of textview textview dynamictext = (textview) findviewbyid(r.id.dynamic_msg); dynamictext.settext(message); setcontentview(r.layout.activity_display_message); // button getactionbar().setdisplayhomeasupenabled(true); }
unfortunately, when press send
button in main activity code throws following exception:
java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference
i've searched on internet , approach adopted in other tutorials looks quite similar, cannot understand i'm doing wrong. thanks!
the content not set view, findviewbyid
return null if before setcontentview
.
move setcontentview
before findviewbyid
, work.
Comments
Post a Comment