android - Why does my app crash on calling a Service from a Broadcast Receiver on a Button click? -
i have developed app in service keeps running in background. service started broadcast receiver after every 3 minutes. broadcast receiver initiated button click. problem getting following errors when click button start broadcast receiver. can tell me going wrong?
my broadcast receiver follows:
public class k extends broadcastreceiver{ public static alarmmanager am; @suppresslint("newapi") @override public void onreceive(context context, intent arg1) { // todo auto-generated method stub intent broadcast = new intent(context, k.class); pendingintent pendingintent = pendingintent.getbroadcast(context, 0, broadcast, 0); alarmmanager alarmmanager = (alarmmanager) context.getsystemservice(context.alarm_service); alarmmanager.setrepeating(alarmmanager.rtc_wakeup,system.currenttimemillis()+1000, 300000, pendingintent); intent = new intent(context,ser.class); context.getapplicationcontext().startservice(i); } }
and service follows:
public class ser extends service implements locationlistener { public location getlocation(string provider) { if (locationmanager.isproviderenabled(provider)) { locationmanager.requestlocationupdates(provider, min_time_for_update, min_distance_for_update, this); if (locationmanager != null) { location = locationmanager.getlastknownlocation(provider); double latitude = location.getlatitude(); double longitude = location.getlongitude(); string l1=string.valueof(latitude); string l2=string.valueof(longitude); } } return location; } @override public int onstartcommand(intent intent, int flags, int startid) { // todo auto-generated method stub //toast.maketext(this, "service starting", toast.length_short).show(); databasehandler db=new databasehandler(getapplicationcontext()); locationmanager = (locationmanager) getapplicationcontext().getsystemservice(location_service); getlocation(locationmanager.network_provider); isgpsenabled = locationmanager.isproviderenabled(locationmanager.gps_provider); isnetworkenabled = locationmanager .isproviderenabled(locationmanager.network_provider); if(isnetworkenabled==true && isgpsenabled==false) { getlocation(locationmanager.network_provider); } else if(isgpsenabled==true && isnetworkenabled==false) { getlocation(locationmanager.gps_provider); } else if(isgpsenabled==true && isnetworkenabled==false) { getlocation(locationmanager.gps_provider); } else if(isgpsenabled==true && isnetworkenabled==true ) { getlocation(locationmanager.gps_provider); } return super.onstartcommand(intent, flags, startid); } @override public void onlocationchanged(location location) { this.location=location; double latitude =location.getlatitude(); double longitude = location.getlongitude(); databasehandler db=new databasehandler(getapplicationcontext()); string l1=string.valueof(latitude); string l2=string.valueof(longitude); val=db.taskid(l1, l2); }
and here error log;
03-21 16:11:37.515: e/androidruntime(22504): fatal exception: main 03-21 16:11:37.515: e/androidruntime(22504): java.lang.runtimeexception: unable start service com.example.ishutup.ser@4105d298 intent { cmp=com.example.ishutup/.ser }: java.lang.nullpointerexception 03-21 16:11:37.515: e/androidruntime(22504): @ android.app.activitythread.handleserviceargs(activitythread.java:2376) 03-21 16:11:37.515: e/androidruntime(22504): @ android.app.activitythread.access$1900(activitythread.java:123) 03-21 16:11:37.515: e/androidruntime(22504): @ android.app.activitythread$h.handlemessage(activitythread.java:1210) 03-21 16:11:37.515: e/androidruntime(22504): @ android.os.handler.dispatchmessage(handler.java:99) 03-21 16:11:37.515: e/androidruntime(22504): @ android.os.looper.loop(looper.java:137) 03-21 16:11:37.515: e/androidruntime(22504): @ android.app.activitythread.main(activitythread.java:4424) 03-21 16:11:37.515: e/androidruntime(22504): @ java.lang.reflect.method.invokenative(native method) 03-21 16:11:37.515: e/androidruntime(22504): @ java.lang.reflect.method.invoke(method.java:511) 03-21 16:11:37.515: e/androidruntime(22504): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 03-21 16:11:37.515: e/androidruntime(22504): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 03-21 16:11:37.515: e/androidruntime(22504): @ dalvik.system.nativestart.main(native method) 03-21 16:11:37.515: e/androidruntime(22504): caused by: java.lang.nullpointerexception 03-21 16:11:37.515: e/androidruntime(22504): @ com.example.ishutup.ser.getlocation(ser.java:40) 03-21 16:11:37.515: e/androidruntime(22504): @ com.example.ishutup.ser.onstartcommand(ser.java:61) 03-21 16:11:37.515: e/androidruntime(22504): @ android.app.activitythread.handleserviceargs(activitythread.java:2359) 03-21 16:11:37.515: e/androidruntime(22504): ... 10 more
maybe location null.
from locationmanager.getlastknownlocation documentation: if provider disabled, null returned
Comments
Post a Comment