OnClickListener in android app fails -
when start android application on smartphone system closing app , says: "sorry. yourapplication closed."
it because of onclicklistener, why?
here code:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button addtodo = (button) findviewbyid((r.id.addtodobutton)); button cancel = (button) findviewbyid(r.id.cancel); cancel.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { system.out.println("seas!"); } }); }
stack trace:
java.lang.runtimeexception: unable start activity componentinfo{sstodo.app/sstodo.app.mainactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2266) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2316) @ android.app.activitythread.access$600(activitythread.java:150) @ android.app.activitythread$h.handlemessage(activitythread.java:1298) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:213) @ android.app.activitythread.main(activitythread.java:5225) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:741) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:557) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ sstodo.app.mainactivity.oncreate(mainactivity.java:24) @ android.app.activity.performcreate(activity.java:5133) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2230)
buttons (used in alet dialog):
<button android:id="@+id/addtodobutton" android:text="@string/add" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:clickable="true" style="@style/addbuttonstyle" /> <button android:id="@+id/cancel" android:text="@string/cancel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:clickable="true" style="@style/cancelbuttonstyle" />
try changing code this
cancel.setonclicklistener(new view. onclicklistener() { @override public void onclick(view view) { system.out.println("seas!"); } });
Comments
Post a Comment