SharedPreferences始终返回null

时间:2016-12-13 18:17:53

标签: android

我有[yurieastwood@mbp ~]$ svn checkout --username yeastwood http://10.0.0.1:8080/svn/SampleProject/trunk/ Authentication realm: <http://10.0.0.1:8080> VisualSVN Server Password for 'yeastwood': ********* A trunk/SampleProject.XmlImporter A trunk/SampleProject.XmlImporter/SampleProject.XmlImporter.csproj A trunk/SampleProject.XmlImporter/JsonConvertTask.cs A trunk/SampleProject.XmlImporter/packages.config A trunk/SampleProject.XmlImporter/bin A trunk/SampleProject.XmlImporter/bin/Release A trunk/SampleProject.XmlImporter/bin/Debug A trunk/SampleProject.XmlImporter/bin/Debug/SampleProject.Framework.pdb A trunk/SampleProject.XmlImporter/bin/Debug/Autorun.mdf A trunk/SampleProject.XmlImporter/bin/Debug/Autorun_log.LDF A trunk/SampleProject.XmlImporter/bin/Debug/SampleProject.XmlImporter.pdb A trunk/SampleProject.XmlImporter/bin/Debug/SampleProject.Framework.dll A trunk/SampleProject.XmlImporter/bin/Debug/Newtonsoft.Json.dll A trunk/SampleProject.XmlImporter/bin/Debug/SampleProject.XmlImporter.dll A trunk/SampleProject.XmlImporter/bin/Debug/Newtonsoft.Json.xml Checked out revision 7. [yurieastwood@mbp ~]$ 将用户名保存到试图访问首选项的ActivityASharedPreference。但是,它始终返回ActivityB

这是我的null :(使用ActivityA进行测试)

MODE_WORLD_READABLE

以下是// I know un is not null String un = data.getExtras().getString("username"); Log.d("un value", un); SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); SharedPreferences.Editor editor = myPrefs.edit(); editor.putString("username", un); editor.apply(); :(日志显示ActivityB值)

null

在查看了许多使用此类的SO示例后,我不确定问题是什么。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

检查一下:

这将有助于维护单独的优先级。使用 PreferencesUtil.class 来处理值。

应用程序扩展类

公共类MyApplication扩展了Application {

/**
 * The constant mApplication.
 */
public static MyApplication mApplication;
public static final String PREFERENCE_NAME = "PREFRENCE_FILE_NAME_1.0";
public static final int PREFERENCE_MODE = 0; //private mode

@Override
public void onCreate() {
    super.onCreate();
    mApplication = this;


}

/**
 * Gets application.
 *
 * @return the application
 */
public static MyApplication getApplication() {
    return mApplication;
}

public static SharedPreferences getSharedPreference() {
    return getApplication().getSharedPreferences(PREFERENCE_NAME, PREFERENCE_MODE);
}

}

PreferenceUtil.class

public class PreferencesUtil {

private SharedPreferences mSharedPreferences = MyApplication.getSharedPreference();
private int DEFAULT_INT= 0;

public PreferencesUtil() {

}

/**
 * Shared Preference.
 **/
// Save strings in preference
public void savePreferences(String key, String value) {
    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}

// Save boolean values in preference
public void savePreferencesBoolean(String key, boolean value) {
    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}

// Save boolean values in preference
public void savePreferencesLong(String key, long value) {
    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.putLong(key, value);
    editor.commit();
}

// Save int values in preference
public void savePreferencesInt(String key, int value) {
    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.putInt(key, value);
    editor.commit();
}

// Get string values from preference
public String getPreferences(String key,String defaultValue) {
    return mSharedPreferences.getString(key, defaultValue);
}

// Get boolean values from preference
public boolean getPreferencesBoolean(String key) {
    return mSharedPreferences.getBoolean(key, false);   //false is default value
}

// Get Long values from preference
public long getPreferencesLong(String key) {
    return mSharedPreferences.getLong(key, DEFAULT_INT);   //false is default value
}

// Get int values from preference
public int getPreferencesInt(String key) {
    return mSharedPreferences.getInt(key, DEFAULT_INT);   //false is default value
}

}

答案 1 :(得分:0)

我认为问题是MODE_WORLD_READABLE,同时将数据放入首选项尝试使用任何其他模式,如MODE_PRIVATE

答案 2 :(得分:0)

保存您的偏好。

String un = data.getExtras().getString("username");

SharedPreferences myPrefs = getSharedPreferences("myPrefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putString("username", un);
editor.commit();

并像这样检索偏好值。

SharedPreferences myPrefs = getSharedPreferences("myPrefs", Activity.MODE_PRIVATE);
String username = myPrefs.getString("username","");
Log.d("Username", "username from store: " + username);