android - context of service is null -
i wrote service give gps location , in location changed want compare location information values shared prefrence , show toast or alart dialog shared prefrence , tost dosnt work because context null. here code , error.please me.thanks
public class broadcastreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { intent service1=new intent(context,gpstracker.class); context.startservice(service1); log.i("service", "service"); } }
and
public class gpstracker extends service implements locationlistener { sharedpreferences pref; @override public void oncreate() { pref=this.getsharedpreferences("mypref",0); super.oncreate(); } @override public void onstart(intent intent, int startid) { pref=this.getsharedpreferences("mypref",0); super.onstart(intent, startid); } private final context mcontext; // flag gps status boolean isgpsenabled = false; // flag network status boolean isnetworkenabled = false; // flag gps status boolean cangetlocation = false; location location; // location double latitude; // latitude double longitude; // longitude // minimum distance change updates in meters private static final long min_distance_change_for_updates = 10; // 10 meters // minimum time between updates in milliseconds private static final long min_time_bw_updates = 1000 * 60 * 1; // 1 minute // declaring location manager protected locationmanager locationmanager; public gpstracker(context context) { this.mcontext = context; getlocation(); } public location getlocation() { try { locationmanager = (locationmanager) mcontext .getsystemservice(location_service); // getting gps status isgpsenabled = locationmanager .isproviderenabled(locationmanager.gps_provider); // getting network status isnetworkenabled = locationmanager .isproviderenabled(locationmanager.network_provider); if (!isgpsenabled && !isnetworkenabled) { // no network provider enabled } else { this.cangetlocation = true; if (isnetworkenabled) { locationmanager.requestlocationupdates( locationmanager.network_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("network", "network"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.network_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } // if gps enabled lat/long using gps services if (isgpsenabled) { if (location == null) { locationmanager.requestlocationupdates( locationmanager.gps_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("gps enabled", "gps enabled"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.gps_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } } } } catch (exception e) { e.printstacktrace(); } return location; } /** * stop using gps listener * calling function stop using gps in app * */ public void stopusinggps(){ if(locationmanager != null){ locationmanager.removeupdates(gpstracker.this); } } /** * function latitude * */ public double getlatitude(){ if(location != null){ latitude = location.getlatitude(); } // return latitude return latitude; } /** * function longitude * */ public double getlongitude(){ if(location != null){ longitude = location.getlongitude(); } // return longitude return longitude; } /** * function check gps/wifi enabled * @return boolean * */ public boolean cangetlocation() { return this.cangetlocation; } /** * function show settings alert dialog * on pressing settings button lauch settings options * */ public void showsettingsalert(){ alertdialog.builder alertdialog = new alertdialog.builder(mcontext); // setting dialog title alertdialog.settitle("gps settings"); // setting dialog message alertdialog.setmessage("gps not enabled. want go settings menu?"); // on pressing settings button alertdialog.setpositivebutton("settings", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { intent intent = new intent(settings.action_location_source_settings); mcontext.startactivity(intent); } }); // on pressing cancel button alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); // showing alert message alertdialog.show(); } public void onlocationchanged(location location) { log.i("servise",boolean.tostring(pref.getboolean("default", false))); if(pref.getboolean("default", false)==true) { string defaultcity=pref.getstring("defaultcity", null); double defaultlat=double.parsedouble(pref.getstring("defaultlat", null)); double defaultlng=double.parsedouble(pref.getstring("defaultlng", null)); geocoder go=new geocoder(gpstracker.this); try { list<address> address=go.getfromlocation(latitude, latitude, 1); if(address.get(0).getadminarea().equals(defaultcity)) { } else { if(latitude==defaultlat && longitude==defaultlng) { } else { string s="your current location has changed.it's better change default place of praytimes application."; toast.maketext(this, s, toast.length_long).show(); string tag="locationchange"; log.i(tag,"locationchanged"); } } } catch (ioexception e) { e.printstacktrace(); } } } public void onproviderdisabled(string provider) { } public void onproviderenabled(string provider) { } public void onstatuschanged(string provider, int status, bundle extras) { } @override public ibinder onbind(intent arg0) { return null; } }
my error log
your error log shows error occurs in line 214 of gpstracker
in gpstracker.onlocationchanged(...)
method. can't tell line is, can, examine line, @ object reference on line, , work why 1 of them null.
Comments
Post a Comment