Tried multiple things I just can't get it why I'm getting the nullPointer. The array myStrings has data in it but it's acting like it doesn't. Anyone spot the problem? Here's the entire code:
public class GPSTracker extends Service implements LocationListener {
private final Context context;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5;
private static final long MIN_TIME_BW_UPDATES = 5000;
private ListViewAdapter urgentTodosAdapter;
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.context = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if(isGPSEnabled) {
if(location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
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;
}
public void stopUsingGPS() {
if(locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude() {
if(location != null) {
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude() {
if(location != null) {
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("GPS is settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
this.location = location;
getLatitude();
getLongitude();
ParseUser user2 = ParseUser.getCurrentUser();
ParseGeoPoint geoPoint = new ParseGeoPoint(getLatitude(), getLongitude());
user2.put("lat_long", geoPoint);
user2.saveInBackground();
Log.d("Coordinates", getLatitude() + " " + getLongitude());
String username = user2.getUsername();
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereWithinKilometers("lat_long", geoPoint, 0.03);
query.whereNotEqualTo("username", username);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> listUsers, ParseException e) {
for (ParseObject object : listUsers){
String ids = object.getString("fullname");
Log.d("These people are here:"," "+ids);
SharedPreferences sharedPrefs = getSharedPreferences("com.parseapp.eseen.eseen", 0);
Boolean onOFF = sharedPrefs.getBoolean("ToggleOnOff",true);
SharedPreferences settings = getSharedPreferences("NotificationIDs", 0);
SharedPreferences.Editor editor = settings.edit();
Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>());
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
if (onOFF) {
int a = 0;
for (String checking : myStrings) {
if (object.getObjectId().equals(checking)) {
a = 1;
}
}
if (a == 1) {
Log.d("This user: ", object.getString("fullname"));
} else {
ParseUser user = ParseUser.getCurrentUser();
ParseQuery query = ParseInstallation.getQuery();
query.whereEqualTo("user", user.getObjectId());
installation.saveInBackground();
ParsePush androidPush = new ParsePush();
androidPush.setMessage(object.getString("fullname") + " is near you!");
androidPush.setQuery(query);
androidPush.sendInBackground();
}
}
myStrings.add(object.getObjectId());
editor.putStringSet("myStrings", myStrings);
editor.apply();
}
}
});
}
Quick explanation, it checks for users with certain criteria, makes a list and then checks if the user is new, if that's true then it sends a notification to the user and saves it on a "History List", where if the user comes up again he will not get a notification.
This is the logcat:
06-11 09:03:38.211 29052-29052/com.parseapp.eseen.eseen E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:153)
at com.parseapp.eseen.eseen.GPSTracker$3.done(GPSTracker.java:200)
at com.parseapp.eseen.eseen.GPSTracker$3.done(GPSTracker.java:192)
at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
Make sure you are invoking
getSharedPreferences()
afteronCreate()
has been called on an Activity.