java - setOnClickListeners() crashes my app -
this question has reply here:
what null pointer exception, , how prepare it? 12 answers nullpointerexception accessing views in oncreate() 8 answersso created new project via eclipse adt , app won't run, here useful info it. grateful if you'd solve it.
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()) .commit(); } final textview reply = (textview) findviewbyid(r.id.textview1); button askbutton = (button) findviewbyid(r.id.button1); askbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub answer.settext("veik"); } }); }
this part of mainactivity.java edited, here edited part of xml file:
<button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:text="button" />
and lastly, message of logcat:
06-19 00:04:56.740: d/androidruntime(7026): shutting downwards vm 06-19 00:04:56.740: w/dalvikvm(7026): threadid=1: thread exiting uncaught exception (group=0x41760ba8) 06-19 00:04:56.740: e/androidruntime(7026): fatal exception: main 06-19 00:04:56.740: e/androidruntime(7026): process: com.example.babyplease, pid: 7026 06-19 00:04:56.740: e/androidruntime(7026): java.lang.runtimeexception: unable start activity componentinfo{com.example.babyplease/com.example.babyplease.mainactivity}: java.lang.nullpointerexception 06-19 00:04:56.740: e/androidruntime(7026): @ android.app.activitythread.performlaunchactivity(activitythread.java:2198) 06-19 00:04:56.740: e/androidruntime(7026): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2248) 06-19 00:04:56.740: e/androidruntime(7026): @ android.app.activitythread.access$800(activitythread.java:138) 06-19 00:04:56.740: e/androidruntime(7026): @ android.app.activitythread$h.handlemessage(activitythread.java:1199) 06-19 00:04:56.740: e/androidruntime(7026): @ android.os.handler.dispatchmessage(handler.java:102) 06-19 00:04:56.740: e/androidruntime(7026): @ android.os.looper.loop(looper.java:136) 06-19 00:04:56.740: e/androidruntime(7026): @ android.app.activitythread.main(activitythread.java:5050) 06-19 00:04:56.740: e/androidruntime(7026): @ java.lang.reflect.method.invokenative(native method) 06-19 00:04:56.740: e/androidruntime(7026): @ java.lang.reflect.method.invoke(method.java:515) 06-19 00:04:56.740: e/androidruntime(7026): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:789) 06-19 00:04:56.740: e/androidruntime(7026): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:605) 06-19 00:04:56.740: e/androidruntime(7026): @ dalvik.system.nativestart.main(native method) 06-19 00:04:56.740: e/androidruntime(7026): caused by: java.lang.nullpointerexception 06-19 00:04:56.740: e/androidruntime(7026): @ com.example.babyplease.mainactivity.oncreate(mainactivity.java:31) 06-19 00:04:56.740: e/androidruntime(7026): @ android.app.activity.performcreate(activity.java:5241) 06-19 00:04:56.740: e/androidruntime(7026): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 06-19 00:04:56.740: e/androidruntime(7026): @ android.app.activitythread.performlaunchactivity(activitythread.java:2162) 06-19 00:04:56.740: e/androidruntime(7026): ... 11 more 06-19 00:04:59.365: i/process(7026): sending signal. pid: 7026 sig: 9
sorry if there much text, i'm confused why doesn't work.
there no textview defined in xml id textview1.
you need add together textview widget resolve nullpointerexception.
<textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true"/>
java android adt
No comments:
Post a Comment