java - ADT simple app - fatal exception -
this question has answer here:
i have started out android , java, , have made simple app couple of textviews, button , edittext.
when run app error saying the: unfortunately, firstapp has stopped. familiar programming, , wonder if miss library, or if adt misconfigured.
i have tried clean , rebuild, right click project in package explorer , android tools -> add support library, restarted emulator, restarted adt , restarted computer. cant figure out this.
can of see wrong here?
mainactivity.java imports
import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.*; import android.os.build;
mainactivity.java
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button btncalc = (button)findviewbyid(r.id.btncalc); btncalc.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { edittext number = (edittext)findviewbyid(r.id.num); textview display = (textview)findviewbyid(r.id.display); double num = double.parsedouble(number.gettext().tostring()); num = num * 5; display.settext(num + ""); } }); 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; }
fragment_main.xml
<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.danlevi.testproject.mainactivity$placeholderfragment" > <textview android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:text="@string/lbltitle" /> <edittext android:id="@+id/num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textview1" android:layout_centerhorizontal="true" android:layout_margintop="15dp" android:ems="10" android:inputtype="numberdecimal|numbersigned" > <requestfocus /> </edittext> <button android:id="@+id/btncalc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/num" android:layout_centerhorizontal="true" android:layout_margintop="14dp" android:text="@string/btncalc" /> <textview android:id="@+id/display" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/btncalc" android:layout_margintop="16dp" /> </relativelayout>
console
[2014-05-16 18:33:16 - testproject] dx trouble writing output: prepared [2014-05-16 18:33:17 - testproject] ------------------------------ [2014-05-16 18:33:17 - testproject] android launch! [2014-05-16 18:33:17 - testproject] adb running normally. [2014-05-16 18:33:17 - testproject] performing com.danlevi.testproject.mainactivity activity launch [2014-05-16 18:33:17 - testproject] automatic target mode: using existing emulator 'emulator-5554' running compatible avd 'android' [2014-05-16 18:33:17 - testproject] uploading testproject.apk onto device 'emulator-5554' [2014-05-16 18:33:18 - testproject] installing testproject.apk... [2014-05-16 18:33:30 - testproject] success! [2014-05-16 18:33:30 - testproject] starting activity com.danlevi.testproject.mainactivity on device emulator-5554 [2014-05-16 18:33:33 - testproject] activitymanager: starting: intent { act=android.intent.action.main cat=[android.intent.category.launcher] cmp=com.danlevi.testproject/.mainactivity }
logcat
05-16 12:33:37.412: d/androidruntime(1579): shutting down vm 05-16 12:33:37.412: w/dalvikvm(1579): threadid=1: thread exiting uncaught exception (group=0xb2b08ba8) 05-16 12:33:37.432: e/androidruntime(1579): fatal exception: main 05-16 12:33:37.432: e/androidruntime(1579): process: com.danlevi.testproject, pid: 1579 05-16 12:33:37.432: e/androidruntime(1579): java.lang.runtimeexception: unable start activity componentinfo{com.danlevi.testproject/com.danlevi.testproject.mainactivity}: java.lang.nullpointerexception 05-16 12:33:37.432: e/androidruntime(1579): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 05-16 12:33:37.432: e/androidruntime(1579): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 05-16 12:33:37.432: e/androidruntime(1579): @ android.app.activitythread.access$800(activitythread.java:135) 05-16 12:33:37.432: e/androidruntime(1579): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 05-16 12:33:37.432: e/androidruntime(1579): @ android.os.handler.dispatchmessage(handler.java:102) 05-16 12:33:37.432: e/androidruntime(1579): @ android.os.looper.loop(looper.java:136) 05-16 12:33:37.432: e/androidruntime(1579): @ android.app.activitythread.main(activitythread.java:5017) 05-16 12:33:37.432: e/androidruntime(1579): @ java.lang.reflect.method.invokenative(native method) 05-16 12:33:37.432: e/androidruntime(1579): @ java.lang.reflect.method.invoke(method.java:515) 05-16 12:33:37.432: e/androidruntime(1579): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 05-16 12:33:37.432: e/androidruntime(1579): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 05-16 12:33:37.432: e/androidruntime(1579): @ dalvik.system.nativestart.main(native method) 05-16 12:33:37.432: e/androidruntime(1579): caused by: java.lang.nullpointerexception 05-16 12:33:37.432: e/androidruntime(1579): @ com.danlevi.testproject.mainactivity.oncreate(mainactivity.java:25) 05-16 12:33:37.432: e/androidruntime(1579): @ android.app.activity.performcreate(activity.java:5231) 05-16 12:33:37.432: e/androidruntime(1579): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 05-16 12:33:37.432: e/androidruntime(1579): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 05-16 12:33:37.432: e/androidruntime(1579): ... 11 more 05-16 12:33:44.352: i/process(1579): sending signal. pid: 1579 sig: 9
this because findviewbyid() searches in activity_main layout, while button located in fragment's layout fragment_main.
move piece of code in oncreateview()
method of fragment:
//... view rootview = inflater.inflate(r.layout.fragment_main, container, false); button buttonclick = (button)rootview.findviewbyid(r.id.button); buttonclick.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { onbuttonclick((button) view); } });
notice access through rootview view
:
button buttonclick = (button)rootview.findviewbyid(r.id.button);
otherwise again nullpointerexception
.
in code, move
changer = (button) findviewbyid(r.id.change1); changeme = (textview) findviewbyid (r.id.changeme1); changer.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub changeme.settext("changed"); } });
to oncreateview()
method of fragment
Comments
Post a Comment