android - Get NullPointerException when use sendbroadcast for GPS location -
after researching through stackoverflow , not finding answer, decided post question.
i trying gps location , broadcasting mainactivity. in mainactivity have this:
//get current location locationmanager locationmanager= (locationmanager)getsystemservice(context.location_service); mycurrentlocationlistener locationlistener = new mycurrentlocationlistener(); locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, (locationlistener) locationlistener);
and mycurrentlocationlistener looks this:
public class mycurrentlocationlistener extends activity implements locationlistener { @override public void onlocationchanged(location location) { location.getlatitude(); location.getlongitude(); string mylocation = "latitude = " + location.getlatitude() + " longitude = " + location.getlongitude(); //i make log see results log.e("my current location", mylocation); intent intent_lat=new intent("loc_lat"); intent_lat.putextra("latitud",location.getlatitude()); sendbroadcast(intent_lat); } @override public void onstatuschanged(string s, int i, bundle bundle) { } @override public void onproviderenabled(string s) { } @override public void onproviderdisabled(string s) { }
}
i've been trying lots of things, such converting double latitude string, making class not extending activity (but way cannot use sendbroadcast).
what shoul do? if need more things tell me.
thank you.
edit: logcat
09-30 12:58:47.513 32019-32019/com.smartglove.uma.smartgloveuma e/my current location: latitude = 37.18993917 longitude = -3.60485555 09-30 12:58:47.515 32019-32019/com.smartglove.uma.smartgloveuma d/androidruntime: shutting down vm 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: fatal exception: main 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: process: com.smartglove.uma.smartgloveuma, pid: 32019 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: java.lang.nullpointerexception: attempt invoke virtual method 'void android.content.context.sendbroadcast(android.content.intent)' on null object reference 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: @ android.content.contextwrapper.sendbroadcast(contextwrapper.java:377) 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: @ com.smartglove.uma.smartgloveuma.mycurrentlocationlistener.onlocationchanged(mycurrentlocationlistener.java:37) 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: @ android.location.locationmanager$listenertransport._handlemessage(locationmanager.java:281) 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: @ android.location.locationmanager$listenertransport.access$000(locationmanager.java:210) 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: @ android.location.locationmanager$listenertransport$1.handlemessage(locationmanager.java:226) 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: @ android.os.handler.dispatchmessage(handler.java:102) 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: @ android.os.looper.loop(looper.java:135) 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: @ android.app.activitythread.main(activitythread.java:5254) 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: @ java.lang.reflect.method.invoke(native method) 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: @ java.lang.reflect.method.invoke(method.java:372) 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) 09-30 12:58:47.518 32019-32019/com.smartglove.uma.smartgloveuma e/androidruntime: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
you need implement locationlistener
interface in mainactivity
, change code to:
locationmanager locationmanager = (locationmanager)getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, this);
Comments
Post a Comment