I am new to Android and app getting terminated when it access to SharedPreferences from a non activity class. Giving my code below, In Debug
mode the code running up to sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
after this line the app got terminated.
package org.traccar.client;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.preference.PreferenceManager;
import java.util.Date;
import static android.content.Context.MODE_PRIVATE;
public class Position {
private static SharedPreferences sharedPreferences;
private Context context;
public Position() {
}
public Position(Context context) {
this.context=context;
}
public Position(String deviceId, Location location, double battery) {
this.deviceId = deviceId;
time = new Date(location.getTime());
latitude = location.getLatitude();
longitude = location.getLongitude();
altitude = location.getAltitude();
speed = location.getSpeed() * 1.943844; // speed in knots
course = location.getBearing();
if (location.getProvider() != null && !location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
accuracy = location.getAccuracy();
}
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean ccflag = sharedPreferences.getBoolean("CheckIn", false);
sharedPreferences.edit().putBoolean("CheckIn", true).apply();
if (ccflag == true){
mock = ccflag;
}
this.battery=battery;
}
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
private String deviceId;
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
private Date time;
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
private double latitude;
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
private double longitude;
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
private double altitude;
public double getAltitude() {
return altitude;
}
public void setAltitude(double altitude) {
this.altitude = altitude;
}
private double speed;
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
private double course;
public double getCourse() {
return course;
}
public void setCourse(double course) {
this.course = course;
}
private double accuracy;
public double getAccuracy() {
return accuracy;
}
public void setAccuracy(double accuracy) {
this.accuracy = accuracy;
}
private double battery;
public double getBattery() {
return battery;
}
public void setBattery(double battery) {
this.battery = battery;
}
private boolean mock;
public boolean getMock() {
return mock;
}
public void setMock(boolean mock) {
this.mock = mock;
}
}
Adding the screenshot of the debug mode here
When i tried first time i couldn't put context there. after googling i found a way to add context in a non activity class
private Context context;
public Position(Context context) {
this.context=context;
}
after adding this code i am able to remove all error. After removing the error also my application getting terminated. Any help is appreciated.