android - Modifying TextView crashes program -
so i've defined layout in xml
:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center"> <textview android:id="@+id/char_name" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </linearlayout>
but when try change in java
program crashes.
public class displaymessageactivity extends activity { @suppresslint("newapi") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); textview charname = (textview) findviewbyid(r.id.char_name); charname.settext("bob"); setcontentview(r.layout.displayname); } }
my app compiles , runs, when switches activity
crashes. if set text in xml
file works fine.
you haven't set content activity. add following in oncreate
:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.your_layout); textview charname = (textview) findviewbyid(r.id.char_name); charname.settext("bob"); }
where your_layout.xml xml file containing textview
id char_name
.
note: should set content before initializing textview charname
.
Comments
Post a Comment