java - setText with onClick button and onCreate crashes app -
how text field change when application launches , how change again when click button?
mainactivity.java
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview mytext=(textview)findviewbyid(r.id.myactivitytextview); mytext.settext("launch text change"); } public void mybuttonclicked(view v){ textview mytext=(textview)findviewbyid(r.id.myactivitytextview); mytext.settext("button change text"); } activity_main.xml
<textview android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/mytext" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="change text" android:id="@+id/mybutton" android:layout_below="@+id/mytext" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:onclick="mybuttonclicked"/>
the textview have in xml has id of mytext. change lookup of
textview mytext=(textview)findviewbyid(r.id.myactivitytextview); to
textview mytext=(textview)findviewbyid(r.id.mytext); and should work.
Comments
Post a Comment