android - How to interpret Debugging information? -


this 2 questions. first relates program, , second more general question regarding debugging.

in code, have 2 button listeners. when first clicked, onclicklistener creates locationlistener, locationmanager requests updates locationlistener. (see below...)

locationmanager locationmanager = (locationmanager)  getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, locationlistener); 

on click of second button, try unregister listener follows...

locationmanager.removeupdates(locationlistener);

however, end force close , following info in debugger...

error/androidruntime(310): fatal exception: main error/androidruntime(310): java.lang.nullpointerexception error/androidruntime(310):     @ com.ryan.gcal.gruncal$2.onclick(gruncal.java:94) error/androidruntime(310):     @ android.view.view.performclick(view.java:2408) error/androidruntime(310):     @ android.view.view$performclick.run(view.java:8816) error/androidruntime(310):     @ android.os.handler.handlecallback(handler.java:587) error/androidruntime(310):     @ android.os.handler.dispatchmessage(handler.java:92) error/androidruntime(310):     @ android.os.looper.loop(looper.java:123) error/androidruntime(310):     @ android.app.activitythread.main(activitythread.java:4627) error/androidruntime(310):     @ java.lang.reflect.method.invokenative(native method) error/androidruntime(310):     @ java.lang.reflect.method.invoke(method.java:521) error/androidruntime(310):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) error/androidruntime(310):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) error/androidruntime(310):     @ dalvik.system.nativestart.main(native method) 

line 94 of code, error originates, tried remove location listener. assume not removing location listener properly, not sure wrong. locationmanager declared @ start of code...

locationmanager locationmanager; 

my first question is, doing wrong in removing listener? , secondly, can provide tips/advice on learning interpret debug errors meaningfully? thanks.

you need either:

a) define locationmanager instance class variable, or retrieve again in onclick.

locationmanager locationmanager;  public void onclick1() {     locationmanager = (locationmanager)  getsystemservice(context.location_service);             locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, locationlistener);  }  public void onclick2() {     locationmanager.removeupdates(locationlistener); } 

you need make sure in onclick1() instead of

locationmanager locationmanager = (locationmanager)  getsystemservice(context.location_service); 

you put

locationmanager = (locationmanager)  getsystemservice(context.location_service); 

specifying locationmanager locationmanager makes scope of declaration enclosed method assign in, rather in scope of class. means in 2nd click locationmanager infact null.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -