java - How to use a sharedPrefs.getString into a Runnable -



java - How to use a sharedPrefs.getString into a Runnable -

how can utilize sharedprefs.getstring runnable ?

private runnable updatecounterthread = new runnable() { public void run() { timeinmilliseconds = systemclock.uptimemillis() - starttime; updatedtime = timeswapbuff + timeinmilliseconds; string tauxhoraire = sharedprefs.getstring("taux_horaire", "null"); double taux=double.parsedouble(tauxhoraire) * 100; int centimes = (int) (updatedtime / 1000 * (taux / 60 / 60)) ; int euros = centimes / 100; centimes = centimes % 100; counterval.settext("" + euros + "," + string.format("%02d", centimes) + " €"); customhandler.postdelayed(this, 0); } };

since when it's error "sharedprefs cannot resolved" can't import in loop.

when add together loop

sharedpreferences sharedprefs = preferencemanager.getdefaultsharedpreferences(this);

i have error "the method getdefaultsharedpreferences(context) in type preferencemanager not applicable arguments (new runnable(){})"

so tried add together first loop

@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.fragment_main); sharedpreferences sharedprefs = preferencemanager.getdefaultsharedpreferences(this); string tauxhoraire = sharedprefs.getstring("taux_horaire", "null");

but resulting first error "sharedprefs cannot resolved"

thanks

in runnable object create there not visibilty of

sharedprefs

on first path used.

in sec path ( 1 throwing the method getdefaultsharedpreferences(context) in type preferencemanager not applicable arguments (new runnable(){})" ) error because of this referred runnable , must referred context, illustration activity.

sharedpreferences sharedprefs = preferencemanager.getdefaultsharedpreferences(getbasecontext());

you can seek follow first path declaring

sharedpreferences sharedprefs

member of activity

public class mainactivity extends activity { private sharedpreferences sharedprefs ;

java android

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -