android - Runtime error on textView.setText(message)... Why? :-( -
i'm new android programming. can say, have been searching answers everywhere on web. please help.
here stackoverflow link of typical, , nearest want accomplish change textview text
my main problem is. whenever want alter text of textview object, runtime error , app "force close". experience in oop in c++ , php. don't understand why app terminate on textview.settext(message). defined. tried textview.settext("hello"). whenever remove statement, app run.
activity_main.xml (entire file)
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="myapp.mainactivity" tools:ignore="mergerootframe" /> fragment_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="myapp.mainactivity$placeholderfragment" > <textview android:id="@+id/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </relativelayout> mainactivity.java
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); string message = "hi there"; textview textview = (textview) findviewbyid(r.id.hello_world); textview.settext("this test"); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } // if } // oncreate // skipped few code /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); homecoming rootview; } // oncreateview } // placeholderfragment logcat
06-23 20:11:42.106: w/dalvikvm(332): threadid=1: thread exiting uncaught exception (group=0x40015560) 06-23 20:11:42.116: e/androidruntime(332): fatal exception: main 06-23 20:11:42.116: e/androidruntime(332): java.lang.runtimeexception: unable start activity componentinfo{myapp/myapp.mainactivity}: java.lang.nullpointerexception 06-23 20:11:42.116: e/androidruntime(332): @ android.app.activitythread.performlaunchactivity(activitythread.java:1647) 06-23 20:11:42.116: e/androidruntime(332): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 06-23 20:11:42.116: e/androidruntime(332): @ android.app.activitythread.access$1500(activitythread.java:117) 06-23 20:11:42.116: e/androidruntime(332): @ android.app.activitythread$h.handlemessage(activitythread.java:931) 06-23 20:11:42.116: e/androidruntime(332): @ android.os.handler.dispatchmessage(handler.java:99) 06-23 20:11:42.116: e/androidruntime(332): @ android.os.looper.loop(looper.java:123) 06-23 20:11:42.116: e/androidruntime(332): @ android.app.activitythread.main(activitythread.java:3683) 06-23 20:11:42.116: e/androidruntime(332): @ java.lang.reflect.method.invokenative(native method) 06-23 20:11:42.116: e/androidruntime(332): @ java.lang.reflect.method.invoke(method.java:507) 06-23 20:11:42.116: e/androidruntime(332): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 06-23 20:11:42.116: e/androidruntime(332): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 06-23 20:11:42.116: e/androidruntime(332): @ dalvik.system.nativestart.main(native method) 06-23 20:11:42.116: e/androidruntime(332): caused by: java.lang.nullpointerexception 06-23 20:11:42.116: e/androidruntime(332): @ myapp.mainactivity.oncreate(mainactivity.java:30) 06-23 20:11:42.116: e/androidruntime(332): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 06-23 20:11:42.116: e/androidruntime(332): @ android.app.activitythread.performlaunchactivity(activitythread.java:1611) 06-23 20:11:42.116: e/androidruntime(332): ... 11 more 06-23 20:11:44.826: i/process(332): sending signal. pid: 332 sig: 9 what nullpointerexception , uncaught exception?
again, app runs when remove settext() line, byt terminates when included. doing wrong?
you calling setcontentview(r.layout.activity_main) in oncreate(). have not provided contents of res/layout/activity_main.xml in question.
however, error indicates res/layout/activity_main.xml not contain widget android:id="@+id/hello_world".
your res/layout/fragment_main.xml file has such widget, not using file in code pasted question far.
either:
add widget right id res/layout/activity_main.xml, or
move textview logic place appropriate, based upon trying utilize res/layout/fragment_main.xml
android textview runtime settext
No comments:
Post a Comment