[英]My app is getting terminated when it tries accessing the shared preferences from a non activity class
我是Android的新手,當它從非活動類訪問SharedPreferences時,應用程序將終止。 在下面的代碼中,在“ Debug
模式下,代碼運行到sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
在此行之后,應用程序終止了。
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;
}
}
當我第一次嘗試時,我無法在此處放置上下文。 谷歌搜索后,我找到了一種在非活動類中添加上下文的方法
private Context context;
public Position(Context context) {
this.context=context;
}
添加此代碼后,我可以刪除所有錯誤。 消除錯誤后,我的應用程序也被終止。 任何幫助表示贊賞。
嘗試替換上下文
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
與“ Position.this”即
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(Position.this);
在使用上下文時 ,許多android開發人員仍然困惑於選擇Activity上下文還是Application上下文。
好吧,選擇合適的環境可能並不那么瑣碎,但這就是我的想法。 如果使用共享首選項,我將檢查保存的信息是否僅在當前視圖生命周期中使用,甚至是銷毀之后也要使用。
如果只是視圖的當前生命周期需要它,則將其傳遞給(Activity)上下文,否則將其傳遞給應用程序上下文。
1.對於Activity Context,通過-> this
2.對於Application Context,通過-> getApplicationContext()
我個人在使用sharedPreference時更喜歡Application Context。 因此,要解決您的問題, sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
您可以簡單地使用PowerPreference
,它將使用應用程序上下文。
每次要使用SharedPreference
時都不需要傳遞上下文
PowerPreference.getDefaultFile().putBoolean("CheckIn", true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.