1

我看到以前有人问过这个问题,但是提供的答案都没有对我有用。

所以,我有两个活动:A 和 B。用户在 A 中插入了一些数据,我希望能够从 B 访问它们(以及将来的更多活动)。我正在尝试使用共享偏好来做到这一点。现在看来,我的代码能够正确地将数据保存在活动 A 中,但是我无法从活动 B 访问相同的 sharedPreference,因为对象(在活动 B 中)为空。看起来它创建了另一个具有相同名称的对象。

我对 android 和 java 很陌生,所以我知道可能只是我不明白这个类是如何工作的,我做错了什么?

活动一

SharedPreferences sharedPref = this.getSharedPreferences("PREF_PERSONAL_DATA",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();

editor.putInt(getString(R.string.n),n);
editor.putInt(getString(R.string.hi), hi);
editor.putInt(getString(R.string.wa), wa);
editor.putInt(getString(R.string.he), he);
editor.putInt(getString(R.string.we), we);
editor.putInt(BlocksNumStr, BlocksNum);
editor.commit();

活动 B

SharedPreferences sharedPref = this.getSharedPreferences("PREF_PERSONAL_DATA", Context.MODE_PRIVATE);

String Weight = sharedPref.getString("we", null);
int W = sharedPref.getInt("weight", 0);

TextView ShowWeight = (TextView) findViewById(R.id.attempt);
ShowWeight.setText(Weight);
TextView ShowW = (TextView) findViewById(R.id.attemptW);
ShowW.setText(W);
4

4 回答 4

1

Android 提供 3 种类型的访问权限SharedPreferences

  1. Activity.getPreferences()- 访问特定于活动的首选项。在您的情况下,这仅对 ActivityA 或 ActivityB 有效。
  2. Activity.getSharedPreferences()或者Context.getSharedPreferences当不在活动中时 - 访问应用程序级别的首选项。这些首选项在您的应用程序中随处可见。
    1. PreferenceManager.getDefaultSharedPreferences()- 访问对安装在 Android 上的每个应用程序可见的全局共享首选项。

从您发布的示例中,一切似乎都是正确的,因为您使用getSharedPreferences(). 我要检查的事情是:

  1. 确保是否String使用来自资源的相同键来检索首选项。
  2. 检查该commit()方法是否不会破坏写入批处理首选项。commit()就个人而言,我在使用方法时遇到了一些偏好未存储的问题。Android Honeycomb 有一个apply()方法。commit()和之间的区别在于,无论您在哪个线程上apply()commit()保存首选项都是apply()异步工作的。如果您不依赖于 Honeycomb 之前的版本,请考虑使用apply().
于 2016-01-26T21:51:06.660 回答
0

A正在存储一个int值,但B正试图将其检索为String- 这应该会导致SharedPreferences.getString()抛出一个ClassCastException. 我推测您的第二个代码片段被捕获此异常的 try/catch 包围。

您必须检索intusing SharedPreferences.getInt()

于 2016-01-26T20:08:55.027 回答
0

确保您使用相同的共享首选项实例的最安全方法是调用

getDefaultSharedPreferences(Context context)

首先将这些添加到您的两个活动 A 和 B

私有 SharedPreference mSharedPreference;私人 SharedPreference.Editor mEditor;

像这样在 onCreate 中启动它们:

mSharedPreference = PreferenceManager.getDefaultSharedPreferences(this);
mEditor = mSharedPreference.edit();

保存到 sharedPreference 这样做:

mEditor.put("KEY",variableToSave);
mEditor.apply();

to read (activity B): //读取之前保存的字符串(例如...)

String readVariable = mSharedPreferences.getString("KEY","DEAULT_VALUE");

使用默认共享首选项将使您能够访问整个应用程序中的文件。因此,您将可以在任何地方(在活动 A 或 B 中)访问使用编辑器保存的变量;

于 2016-01-26T21:15:37.083 回答
0

活动一:

SharedPreferences sharedPref =     getBaseContext().getSharedPreferences("PREF_PERSONAL_DATA",getBaseContext().MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();

editor.putInt(getString(R.string.n),n);
editor.putInt(getString(R.string.hi), hi);
editor.putInt(getString(R.string.wa), wa);
editor.putInt(getString(R.string.he), he);
editor.putInt(getString(R.string.we), we);
editor.putInt(BlocksNumStr, BlocksNum);
editor.commit();

活动 B:

SharedPreferences sharedPref = getBaseContext().getSharedPreferences("PREF_PERSONAL_DATA", getBaseContext().MODE_PRIVATE);

String Weight = sharedPref.getString(getString(R.string.wa), null);
int W = sharedPref.getInt("weight", 0);

TextView ShowWeight = (TextView) findViewById(R.id.attempt);
ShowWeight.setText(Weight);
TextView ShowW = (TextView) findViewById(R.id.attemptW);
ShowW.setText(W);
于 2016-01-26T21:19:05.467 回答