私はAndroid開発の初心者であり、onCreate中に共有設定を使用してdoubleの値を取得しようとしています。ただし、これを行うためのコードは、アプリを初めて再起動したときにのみ機能するようです。その後の再起動中に、コードが機能せず、コードの if/else 部分をスキップします。これは非常にイライラさせられます。どんな助けでも大歓迎です。
public void LoadPreferences(){
double portfoliocurrency1 = null;
SharedPreferences portfoliopreferences = getPreferences(MODE_PRIVATE);
String isuser1created = portfoliopreferences.getString("U1C", "");
if(isuser1created.equals("yes")){
String savedportfolioname = portfoliopreferences.getString("PN1", "");
String saveddenomination = portfoliopreferences.getString("DN1", "");
String savedporfoliocurrency = portfoliopreferences.getString("PC1", ""); //<--Always either "usd", "inr", or "eur".
Log.d("test","SPConLoad=" + savedporfoliocurrency ); //<--Returns correct value on every restart.
Log.d("test","USD=" + currencyUSD ); //<--Returns correct value on every restart.
Log.d("test","EUR=" + currencyEUR ); //<--Returns correct value on every restart.
pn1 = savedportfolioname;
denomination1 = saveddenomination;
if(savedporfoliocurrency=="usd"){ //<--If/else statement only works on first restart. For some reason it is skipped on subsequent restarts.
portfoliocurrency1 = currencyUSD;
Log.d("test","PC1USD=" + currencyUSD ); //<--Returns correct value on first restart.
}else if(savedporfoliocurrency=="eur"){
portfoliocurrency1 = currencyEUR;
Log.d("test","PC1EUR=" + currencyEUR ); //<--Returns correct value on first restart.
}else if(savedporfoliocurrency=="inr"){
portfoliocurrency1 = currencyINR; //<--Returns correct value on first restart.
}
Log.d("test","PC1onLoad=" + portfoliocurrency1 ); //<-- Only returns correct value during first restart. Always returns null on subsequent restarts.
}