当我尝试从非活动类访问共享首选项时,我的应用程序将终止

时间:2018-12-08 07:19:10

标签: java android sharedpreferences

我是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;
    }

}

The error on Android Studio

Adding the screenshot of the debug mode here

当我第一次尝试时,我无法在此处放置上下文。谷歌搜索后,我找到了一种在非活动类中添加上下文的方法

private Context context;
public Position(Context context) {
        this.context=context;
    }

添加此代码后,我可以删除所有错误。消除错误后,我的应用程序也被终止。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:0)

尝试替换

中的上下文
sharedPreferences  = PreferenceManager.getDefaultSharedPreferences(context);

带有“ Position.this”的

sharedPreferences  = PreferenceManager.getDefaultSharedPreferences(Position.this);

答案 1 :(得分:0)

在使用 context 时,许多android开发人员仍然困惑选择 活动上下文或应用程序上下文。

好吧,选择合适的环境可能并不那么琐碎,但这就是我的想法。 如果使用共享首选项,我将检查保存的信息是仅用于当前视图的生命周期还是在销毁视图之后使用。

如果仅用于视图的当前生命周期需要,则传递(Activity)上下文,否则 向其传递应用程序上下文。

1。对于活动上下文,请通过-> this

2。对于应用程序上下文,请通过-> getApplicationContext()

我个人在使用 sharedPreference 时更喜欢应用程序上下文。 所以要解决您的问题, sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

答案 2 :(得分:0)

您可以简单地使用PowerPreference,它将使用应用程序上下文。

每次使用SharedPreference时都不需要传递上下文

   PowerPreference.getDefaultFile().putBoolean("CheckIn", true);

https://github.com/AliAsadi/Android-Power-Preference