java - Android application has stopped working -eclipse -
i have been tried build first android application. every time open application in emulator, message says test has stopped working. must simple. hope can me.
04-06 15:43:47.806: w/dalvikvm(4275): threadid=1: thread exiting uncaught exception (group=0xa614d908)
04-06 15:43:47.826: e/androidruntime(4275): fatal exception: main
04-06 15:43:47.826: e/androidruntime(4275): java.lang.runtimeexception: unable start activity componentinfo{com.test/com.test.mainactivity}: java.lang.nullpointerexception
04-06 15:43:47.826: e/androidruntime(4275): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180)
04-06 15:43:47.826: e/androidruntime(4275): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230)
04-06 15:43:47.826: e/androidruntime(4275): @ android.app.activitythread.access$600(activitythread.java:141)
04-06 15:43:47.826: e/androidruntime(4275): @ android.app.activitythread$h.handlemessage(activitythread.java:1234)
04-06 15:43:47.826: e/androidruntime(4275): @ android.os.handler.dispatchmessage(handler.java:99)
04-06 15:43:47.826: e/androidruntime(4275): @ android.os.looper.loop(looper.java:137)
04-06 15:43:47.826: e/androidruntime(4275): @ android.app.activitythread.main(activitythread.java:5041)
04-06 15:43:47.826: e/androidruntime(4275): @ java.lang.reflect.method.invokenative(native method)
04-06 15:43:47.826: e/androidruntime(4275): @ java.lang.reflect.method.invoke(method.java:511)
04-06 15:43:47.826: e/androidruntime(4275): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793)
04-06 15:43:47.826: e/androidruntime(4275): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560)
04-06 15:43:47.826: e/androidruntime(4275): @ dalvik.system.nativestart.main(native method)
04-06 15:43:47.826: e/androidruntime(4275): caused by: java.lang.nullpointerexception
04-06 15:43:47.826: e/androidruntime(4275): @ com.test.mainactivity.oncreate(mainactivity.java:30)
04-06 15:43:47.826: e/androidruntime(4275): @ android.app.activity.performcreate(activity.java:5104)
04-06 15:43:47.826: e/androidruntime(4275): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080)
04-06 15:43:47.826: e/androidruntime(4275): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144)
04-06 15:43:47.826: e/androidruntime(4275): ... 11 more
above log file
public class main extends actionbaractivity { int counter ; button add, sub ; textview display; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); counter = 0; add = (button) findviewbyid (r.id.badd); sub = (button) findviewbyid (r.id.bsub); display = (textview) findviewbyid(r.id.tvdisplay); add.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub counter++; display.settext("your total " + counter); } }); sub.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub counter --; display.settext("your total " + counter); } }); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * 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); return rootview; } } }
above main class
<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.test.mainactivity$placeholderfragment" > <textview android:id="@+id/tvdisplay" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignleft="@+id/bsub" android:layout_alignparentbottom="true" android:layout_below="@+id/bsub" android:gravity="center" android:text="your total 0" android:textsize="45dp" /> <button android:id="@+id/badd" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="16dp" android:layout_margintop="78dp" android:gravity="center" android:text="add one" android:textsize="20dp" /> <button android:id="@+id/bsub" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_torightof="@+id/badd" android:layout_centerhorizontal="true" android:gravity="center" android:text="subtract one" android:textsize="20dp" /> </relativelayout>
above fragment_main.xml
<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="com.hello.firstapp.main" tools:ignore="mergerootframe" />
above activity_main.xml
instead of oncreate()
move ur initialization oncreateview()
buttons
, textview
in ur fragment_main.xml
thats why u got npe
buttons
, textview
not part of activity_main.xml
.
@override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); counter = 0; add = (button) rootview.findviewbyid (r.id.badd); sub = (button) rootview.findviewbyid (r.id.bsub); display = (textview) rootview.findviewbyid(r.id.tvdisplay); add.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub counter++; display.settext("your total " + counter); } }); sub.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub counter --; display.settext("your total " + counter); } }); return rootview; }
Comments
Post a Comment