为什么共享首选项返回null?

时间:2017-11-19 05:39:55

标签: java android object sharedpreferences

我一直在处理一个我似乎无法弄清楚的奇怪错误。

我想让我的主要活动更小,我将我的共享首选项放在它自己的类中,以便它更有条理。

到目前为止,我的共享首选项属于我尝试使用它的同一个类,并且它工作正常。

但是当我复制相同的代码创建和它的对象并调用它时,我得到一个空对象引用。

但是,出于某种原因,如果我只是把它放在一个子类中就可以了。

非常感谢任何帮助!

public class SharedPref extends Activity {

public void write_to_key(String shared_pref, String pref_key, String value_of_key) {
    //This block of code will automatically store the key and value in the shared preferences
    //All you need to do is provide the sharedpreference, the key you wish to save the value as, and the value
    SharedPreferences sharedpreferences = getSharedPreferences(shared_pref, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putString(pref_key, value_of_key);
    editor.apply();
}

public String read_from_key(String shared_pref, String pref_key) {
    //This block of code will automatically read the value in the shared preferences with the provided key
    //All you need to do is provide the key you want to read and the value will be returned as a string
    SharedPreferences shared = getSharedPreferences(shared_pref, MODE_PRIVATE);
    return (shared.getString(pref_key, ""));
}

}

这是我的课程,我试图通过调用导航

来调用它
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loading_page_layout);
    //Get Wallet keys

    SharedPref sharePref = new SharedPref();
    walletKey = sharePref.read_from_key(MyPREFERENCES, "walletKey");
    WalletType = sharePref.read_from_key(MyPREFERENCES, "walletType");
    System.out.println(walletKey + " " + WalletType + " THESE ARE THE LOGS FOR SHARED PREFERENCES");


    //calling the broadcast receiver method to check internet in background
    this.registerReceiver(this.mConnReceiver,
            new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}

1 个答案:

答案 0 :(得分:0)

您需要传递上下文,而不是使用Activity

继承
import android.content.Context;
import android.content.SharedPreferences;
import static android.content.Context.MODE_PRIVATE;

public class SharedPref {

    private Context mContext;

    public SharedPref(Context context){
        mContext = context;
    }

    public void write_to_key(String shared_pref, String pref_key, String value_of_key) {
        //This block of code will automatically store the key and value in the shared preferences
        //All you need to do is provide the sharedpreference, the key you wish to save the value as, and the value
        SharedPreferences sharedpreferences = mContext.getSharedPreferences(shared_pref, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString(pref_key, value_of_key);
        editor.commit();
    }

    public String read_from_key(String shared_pref, String pref_key) {
        //This block of code will automatically read the value in the shared preferences with the provided key
        //All you need to do is provide the key you want to read and the value will be returned as a string
        SharedPreferences shared = mContext.getSharedPreferences(shared_pref, MODE_PRIVATE);
        return (shared.getString(pref_key, ""));
    }

}

在构造函数中传递上下文

SharedPref sharePref = new SharedPref(this);
String walletKey = sharePref.read_from_key("MyPREFERENCES", "walletKey");
String WalletType = sharePref.read_from_key("MyPREFERENCES", "walletType");