NullPointerException In Ecclipse Android Sdk Unfortunately myapp has stopped -
i beginner. stuck on nullpointerexception. simple program of buttons. run android application, project opens , gets closed displaying unfortunately app has stopped.
04-15 17:40:05.656: d/androidruntime(599): shutting down vm 04-15 17:40:05.656: w/dalvikvm(599): threadid=1: thread exiting uncaught exception (group=0xb2adcd58) 04-15 17:40:05.666: e/androidruntime(599): fatal exception: main 04-15 17:40:05.666: e/androidruntime(599): process: com.example.buttona, pid: 599 04-15 17:40:05.666: e/androidruntime(599): java.lang.runtimeexception: unable start activity componentinfo{com.example.buttona/com.example.buttona.buttonwa}: java.lang.nullpointerexception 04-15 17:40:05.666: e/androidruntime(599): @ android.app.activitythread.performlaunchactivity(activitythread.java:2208) 04-15 17:40:05.666: e/androidruntime(599): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2270) 04-15 17:40:05.666: e/androidruntime(599): @ android.app.activitythread.access$800(activitythread.java:138) 04-15 17:40:05.666: e/androidruntime(599): @ android.app.activitythread$h.handlemessage(activitythread.java:1209) 04-15 17:40:05.666: e/androidruntime(599): @ android.os.handler.dispatchmessage(handler.java:102) 04-15 17:40:05.666: e/androidruntime(599): @ android.os.looper.loop(looper.java:136) 04-15 17:40:05.666: e/androidruntime(599): @ android.app.activitythread.main(activitythread.java:5042) 04-15 17:40:05.666: e/androidruntime(599): @ java.lang.reflect.method.invokenative(native method) 04-15 17:40:05.666: e/androidruntime(599): @ java.lang.reflect.method.invoke(method.java:515) 04-15 17:40:05.666: e/androidruntime(599): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:776) 04-15 17:40:05.666: e/androidruntime(599): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) 04-15 17:40:05.666: e/androidruntime(599): @ dalvik.system.nativestart.main(native method) 04-15 17:40:05.666: e/androidruntime(599): caused by: java.lang.nullpointerexception 04-15 17:40:05.666: e/androidruntime(599): @ com.example.buttona.buttonwa.oncreate(buttonwa.java:29) 04-15 17:40:05.666: e/androidruntime(599): @ android.app.activity.performcreate(activity.java:5231) 04-15 17:40:05.666: e/androidruntime(599): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 04-15 17:40:05.666: e/androidruntime(599): @ android.app.activitythread.performlaunchactivity(activitythread.java:2172) 04-15 17:40:05.666: e/androidruntime(599): ... 11 more 04-15 17:40:11.466: i/process(599): sending signal. pid: 599 sig: 9 04-15 17:57:32.017: d/androidruntime(662): shutting down vm 04-15 17:57:32.017: w/dalvikvm(662): threadid=1: thread exiting uncaught exception (group=0xb2adcd58) 04-15 17:57:32.027: e/androidruntime(662): fatal exception: main 04-15 17:57:32.027: e/androidruntime(662): process: com.example.buttona, pid: 662 04-15 17:57:32.027: e/androidruntime(662): java.lang.runtimeexception: unable start activity componentinfo{com.example.buttona/com.example.buttona.buttonwa}: java.lang.nullpointerexception 04-15 17:57:32.027: e/androidruntime(662): @ android.app.activitythread.performlaunchactivity(activitythread.java:2208) 04-15 17:57:32.027: e/androidruntime(662): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2270) 04-15 17:57:32.027: e/androidruntime(662): @ android.app.activitythread.access$800(activitythread.java:138) 04-15 17:57:32.027: e/androidruntime(662): @ android.app.activitythread$h.handlemessage(activitythread.java:1209) 04-15 17:57:32.027: e/androidruntime(662): @ android.os.handler.dispatchmessage(handler.java:102) 04-15 17:57:32.027: e/androidruntime(662): @ android.os.looper.loop(looper.java:136) 04-15 17:57:32.027: e/androidruntime(662): @ android.app.activitythread.main(activitythread.java:5042) 04-15 17:57:32.027: e/androidruntime(662): @ java.lang.reflect.method.invokenative(native method) 04-15 17:57:32.027: e/androidruntime(662): @ java.lang.reflect.method.invoke(method.java:515) 04-15 17:57:32.027: e/androidruntime(662): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:776) 04-15 17:57:32.027: e/androidruntime(662): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) 04-15 17:57:32.027: e/androidruntime(662): @ dalvik.system.nativestart.main(native method) 04-15 17:57:32.027: e/androidruntime(662): caused by: java.lang.nullpointerexception 04-15 17:57:32.027: e/androidruntime(662): @ com.example.buttona.buttonwa.oncreate(buttonwa.java:29) 04-15 17:57:32.027: e/androidruntime(662): @ android.app.activity.performcreate(activity.java:5231) 04-15 17:57:32.027: e/androidruntime(662): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 04-15 17:57:32.027: e/androidruntime(662): @ android.app.activitythread.performlaunchactivity(activitythread.java:2172) 04-15 17:57:32.027: e/androidruntime(662): ... 11 more 04-15 17:57:34.957: i/process(662): sending signal. pid: 662 sig: 9 this java file.
public class buttonwa extends activity { button name, surname; textview display; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_buttonwa); name = (button) findviewbyid(r.id.name_button); surname = (button) findviewbyid(r.id.surname_button); display = (textview) findviewbyid(r.id.display); name.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub display.settext("your name shashank yadav"); } }); surname.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub display.settext("your name yadav shashank"); } }); this xml file.
<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="com.example.buttona.buttonwa$placeholderfragment" > <textview android:id="@+id/display" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="50dp" /> <button android:id="@+id/name_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_margintop="10dp" android:text="@string/name" /> <button android:id="@+id/surname_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_below="@+id/name_button" android:text="@string/surname" /> </relativelayout>
the problem have created view elements in fragment fragment_main.xml (i assume that's it's called, haven't posted name) code expects them in activity.
when this:
setcontentview(r.layout.activity_buttonwa); name = (button) findviewbyid(r.id.name_button); surname = (button) findviewbyid(r.id.surname_button); display = (textview) findviewbyid(r.id.display); you saying, "hey activity, check activity_buttonwa.xml these view elements, you'll find them there.
judging error message, name null , therefore getting nullpointerexception when try , set onclicklistener on it. it's null because it's not in activity_buttonwa.xml file.
to fix, can either move xml activity_buttonwa.xml or can move inflation code fragment:
/** * 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); name = (button) rootview.findviewbyid(r.id.name_button); surname = (button) rootview.findviewbyid(r.id.surname_button); display = (textview) rootview.findviewbyid(r.id.display); // add onclicklisteners, etc. return rootview; } } i it's better move inflation code fragment, above.
Comments
Post a Comment