android - Cannot Retrieve Location's longitude an latitude -
when try run code, error:
java.lang.nullpointerexception: attempt invoke virtual method 'android.content.res.resources android.content.context.getresources()' on null object reference
i can manually configure coordinates in emulator, still, got same error.
here codes getting location coordinates.
locationtracker.java
public class locationtracker extends mainactivity implements locationlistener { mainactivity activity; context context; location location; locationmanager lm; double lat,longi; boolean isgpsenabled = false; boolean isnetworkenabled = false; boolean cangetnetworklocation = false; public locationtracker (context context) { this.context=context; getgpslocation(); } public location getgpslocation() { lm = (locationmanager) context.getsystemservice(context.location_service); isgpsenabled = lm.isproviderenabled(locationmanager.gps_provider); if (isgpsenabled) { /** check runtime permission */ if (contextcompat.checkselfpermission(context, manifest.permission.access_fine_location) == packagemanager.permission_granted || contextcompat.checkselfpermission(context, manifest.permission.access_coarse_location) == packagemanager.permission_granted) { if (location == null) { //request new data coming gps lm.requestlocationupdates(locationmanager.gps_provider,0,0,this); if (lm != null) { //retrieve data earlier location=lm.getlastknownlocation(locationmanager.gps_provider); if (location != null) { lat= location.getlatitude(); longi= location.getlongitude(); } } } }else { activitycompat.requestpermissions(activity,new string[]{android.manifest.permission.access_coarse_location, manifest.permission.access_fine_location},123); } }else { } return location; } /** latitude method */ public double getlatitude() { if (location != null) { lat=location.getlatitude(); } return lat; } /** longitude method */ public double getlongitude () { if (location != null) { longi=location.getlongitude(); } return longi; } @override public void onlocationchanged(location location) { lat= location.getlatitude(); longi=location.getlongitude(); } @override public void onstatuschanged(string provider, int status, bundle extras) { } @override public void onproviderenabled(string provider) { } @override public void onproviderdisabled(string provider) { } }
and mainactivity class displaying coordinates.
public class mainactivity extends appcompatactivity { context context; dbhelper helper; locationtracker tracker; textview contactstv,sendemergencytv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); helper=new dbhelper(getapplicationcontext()); /** * onclick listener in android */ contactstv=(textview) findviewbyid(r.id.tv_createcontacts); contactstv.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent= new intent(mainactivity.this, contactsactivity.class); //redirect contacts dsadsadsa startactivity(intent); } }); sendemergencytv=(textview)findviewbyid(r.id.tv_sendemergency); sendemergencytv.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { tracker= new locationtracker(mainactivity.this); double lat,longi; lat= tracker.getlatitude(); longi= tracker.getlongitude(); toast.maketext(context,"latitude: "+lat+ "longitude: "+longi,toast.length_long).show(); } }); } }
by way, not sure of placing mainactivity instance inside locationtracker class in requestpermission parameter.
here:
activitycompat.requestpermissions(activity, new string[]{android.manifest.permission.access_coarse_location, manifest.permission.access_fine_location},123);
line causing issue because activity
null
.
use ((activity)context)
instead of activity
.like:
activitycompat.requestpermissions(((activity)context), new string[]{android.manifest.permission.access_coarse_location, manifest.permission.access_fine_location},123);
note:
also, because locationtracker
normal java class no need extends activity
in it.
Comments
Post a Comment