android - Passing integer between activities using shared preferences -
i'm trying pass score
integer 1 activity another. i'm having difficulties shared preferences. in mainactivity don't receive warnings or errors in scoreactivity receive error saying the method getint(string, int) in type shared preferences not applicable arguments (long, int). how fix this?
mainactivity
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();
scoreactivity
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);
this
long score = sharedpref.getint(score, defaultvalue); // in case score int
should be
int score = sharedpref.getint("score", defaultvalue); //"score" key // return's int value not long
check docs
public abstract int getint (string key, int defvalue) added in api level 1 retrieve int value preferences. parameters key name of preference retrieve. defvalue value return if preference not exist. returns returns preference value if exists, or defvalue. throws classcastexception if there preference name not int. throws classcastexception
Comments
Post a Comment