android - E/AndroidRuntime(1549): java.lang.RuntimeException: Unable to start activity ComponentInfo: -
here modified code: but, still getting same error. please help, doing wrong.
public class mainactivity extends actionbaractivity { int counter; button addone; button subone; textview display; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); addone=(button) findviewbyid(r.id.badd); subone= (button) findviewbyid(r.id.bsub); display =(textview) findviewbyid(r.id.tv1); counter=0; addone.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub counter++; display.settext("counter is"+ counter); } }); subone.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub counter--; display.settext("counter "+ counter); //display.settext("counter is"+ counter); } }); }
my xml code:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bat" android:orientation="vertical" 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.iitb.practice.mainactivity$placeholderfragment" > <textview android:id="@+id/tv1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="@string/total" android:textsize="35sp" > </textview> <button android:id="@+id/bsub" android:layout_width="fill_parent" android:layout_height="45dp" android:text="@string/sub" /> <button android:id="@+id/badd" android:layout_width="fill_parent" android:layout_height="45dp" android:text="@string/add" />
i getting following error: logcat:
05-19 03:42:58.520: d/androidruntime(1281): shutting down vm
05-19 03:42:58.520: w/dalvikvm(1281): threadid=1: thread exiting uncaught exception (group=0xb3a96ba8) 05-19 03:42:58.540: e/androidruntime(1281): fatal exception: main 05-19 03:42:58.540: e/androidruntime(1281): process: com.iitb.practice, pid: 1281 05-19 03:42:58.540: e/androidruntime(1281): java.lang.runtimeexception: unable start activity componentinfo{com.iitb.practice/com.iitb.practice.mainactivity}: java.lang.nullpointerexception 05-19 03:42:58.540: e/androidruntime(1281): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 05-19 03:42:58.540: e/androidruntime(1281): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 05-19 03:42:58.540: e/androidruntime(1281): @ android.app.activitythread.access$800(activitythread.java:135) 05-19 03:42:58.540: e/androidruntime(1281): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 05-19 03:42:58.540: e/androidruntime(1281): @ android.os.handler.dispatchmessage(handler.java:102) 05-19 03:42:58.540: e/androidruntime(1281): @ android.os.looper.loop(looper.java:136) 05-19 03:42:58.540: e/androidruntime(1281): @ android.app.activitythread.main(activitythread.java:5017) 05-19 03:42:58.540: e/androidruntime(1281): @ java.lang.reflect.method.invokenative(native method) 05-19 03:42:58.540: e/androidruntime(1281): @ java.lang.reflect.method.invoke(method.java:515) 05-19 03:42:58.540: e/androidruntime(1281): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 05-19 03:42:58.540: e/androidruntime(1281): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 05-19 03:42:58.540: e/androidruntime(1281): @ dalvik.system.nativestart.main(native method) 05-19 03:42:58.540: e/androidruntime(1281): caused by: java.lang.nullpointerexception 05-19 03:42:58.540: e/androidruntime(1281): @ com.iitb.practice.mainactivity.oncreate(mainactivity.java:39) 05-19 03:42:58.540: e/androidruntime(1281): @ android.app.activity.performcreate(activity.java:5231) 05-19 03:42:58.540: e/androidruntime(1281): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 05-19 03:42:58.540: e/androidruntime(1281): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 05-19 03:42:58.540: e/androidruntime(1281): ... 11 more 05-19 03:47:59.190: i/process(1281): sending signal. pid: 1281 sig: 9
you need first call "setcontentview()" inside oncreate() before initialize variables.
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); addone=(button)findviewbyid(r.id.badd); subone= (button) findviewbyid(r.id.bsub); display =(textview) findviewbyid(r.id.tv1);
Comments
Post a Comment