Android 使用共享首选项在活动之间传递整数

Android 使用共享首选项在活动之间传递整数,android,Android,我正在尝试将这个分数整数从一个活动传递到另一个活动。我在分享偏好方面遇到了困难。在MainActivity中,我没有收到任何警告或错误,但在ScoreActivity中,我收到一个错误,表示类型共享首选项中的方法getInt(String,int)不适用于参数(long,int)。我该如何解决这个问题 main活动 private int SCORE = 0; @Override protected void onCreate (Bundle savedInstanceState) {

我正在尝试将这个
分数
整数从一个活动传递到另一个活动。我在分享偏好方面遇到了困难。在MainActivity中,我没有收到任何警告或错误,但在ScoreActivity中,我收到一个错误,表示类型共享首选项中的方法getInt(String,int)不适用于参数(long,int)。我该如何解决这个问题

main活动

private int SCORE = 0;


@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt("SCORE", SCORE);
    editor.commit();
评分活动

private int SCORE = 0;

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_score);


    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    int defaultValue = getResources().getInteger(SCORE);
    long SCORE = sharedPref.getInt(SCORE, defaultValue);
这个

应该是

int SCORE = sharedPref.getInt("SCORE", defaultValue); 
//"SCORE" is the key
// return's a int value not long
检查文件

public abstract int getInt (String key, int defValue)

Added in API level 1
Retrieve an int value from the preferences.

Parameters
key The name of the preference to retrieve.
defValue    Value to return if this preference does not exist.
Returns
Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not an int.
Throws
ClassCastException  

请尝试从设备卸载应用程序,然后重试…@Lal由于上述错误,它将无法编译。
public abstract int getInt (String key, int defValue)

Added in API level 1
Retrieve an int value from the preferences.

Parameters
key The name of the preference to retrieve.
defValue    Value to return if this preference does not exist.
Returns
Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not an int.
Throws
ClassCastException