Android Application Activity NullPointerException -
i'm new android development , stackoverflow, have little experience in programming. i'm trying run a basic application on android emulator, , above mentioned error report. have narrowed problem 1 of these lines of code, saw on web looks i'm using these methods correctly. know error in here somewhere because ran code without , worked. hope i'm being specific enough (i'm open comments on usage of site).
here code:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } counter = 0; add = (button) findviewbyid(r.id.badd); subtract = (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); } }); subtract.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub counter--; display.settext("your total " + counter); } }); } this stack trace logcat:
03-18 08:51:59.379: e/androidruntime(1851): fatal exception: main 03-18 08:51:59.379: e/androidruntime(1851): process: j.g.thenewboston, pid: 1851 03-18 08:51:59.379: e/androidruntime(1851): java.lang.runtimeexception: unable start activity componentinfo{j.g.thenewboston/j.g.thenewboston.mainactivity}: java.lang.nullpointerexception 03-18 08:51:59.379: e/androidruntime(1851): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 03-18 08:51:59.379: e/androidruntime(1851): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 03-18 08:51:59.379: e/androidruntime(1851): @ android.app.activitythread.access$800(activitythread.java:135) 03-18 08:51:59.379: e/androidruntime(1851): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 03-18 08:51:59.379: e/androidruntime(1851): @ android.os.handler.dispatchmessage(handler.java:102) 03-18 08:51:59.379: e/androidruntime(1851): @ android.os.looper.loop(looper.java:136) 03-18 08:51:59.379: e/androidruntime(1851): @ android.app.activitythread.main(activitythread.java:5017) 03-18 08:51:59.379: e/androidruntime(1851): @ java.lang.reflect.method.invokenative(native method) 03-18 08:51:59.379: e/androidruntime(1851): @ java.lang.reflect.method.invoke(method.java:515) 03-18 08:51:59.379: e/androidruntime(1851): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 03-18 08:51:59.379: e/androidruntime(1851): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 03-18 08:51:59.379: e/androidruntime(1851): @ dalvik.system.nativestart.main(native method) 03-18 08:51:59.379: e/androidruntime(1851): caused by: java.lang.nullpointerexception 03-18 08:51:59.379: e/androidruntime(1851): @ j.g.thenewboston.mainactivity.oncreate(mainactivity.java:34) 03-18 08:51:59.379: e/androidruntime(1851): @ android.app.activity.performcreate(activity.java:5231) 03-18 08:51:59.379: e/androidruntime(1851): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 03-18 08:51:59.379: e/androidruntime(1851): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) thanks lot guys
as per comments, looks following returning null , setting add null;
add = (button) findviewbyid(r.id.badd); subtract = (button) findviewbyid(r.id.bsub); display = (textview) findviewbyid(r.id.tvdisplay); add.setonclicklistener(new view.onclicklistener(){}) // line 34 so on line 34 call setonclicklistener throwing nullpointerexception trying interact null object.
if ids placeholderfragment layout, may not accessible activity class yet when findviewbyid called. when call commit() on fragment, action scheduled not carried out immediately. such, can't guarantee methods ever find anything.
any code initializing/manipulating views placeholderfragment layout needs in placeholderfragment class prevent happening.
Comments
Post a Comment