0

この件名でいくつかの投稿を見て、うまくいかなかった解決策を試してみてください。

私は主な活動に書いた:

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);;
setTheme(sharedPreferences.getInt("selectedTheme", 0));

常に 0 を返します。

私はエミュレータ内のSetting.xmlファイルをチェックしました:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<int name="STheme" value="2131099649" />
<int name="selectedTheme" value="2131099649" />
<string name="SThemeS">R.style.AppTheme</string>
</map>

あなたが見ることができるように、キー値「selectedTheme」があります。

何か案は ?

別の問題:作成する必要がありますか

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);;

sharedPreferences を使用する各アクティビティに対して ?

4

1 に答える 1

2

when SharedPreferences created the name of the file is as the activity that created him .

so if you create SharedPreferences in Setting activity and you want to get preferences from other activity you have to spicify the name of SharedPreferences file .

if you trying to get preferences from same activity you dont need to do that .

example : SharedPreferences created in Setting activiy and reading it from diffrent activity :

add this line in current activity -

SharedPreferences preferences = context.getSharedPreferences("Setting",
                MODE_PRIVATE)

and now you can pull the data....

于 2013-02-21T17:04:31.707 に答える