Android - 使用当前设置的首选项初始化变量
Android - Initialize variable with currently set preference
private int mShuffleMode = SharedPreferences.getInt("shufflemode");
告诉我不能从静态上下文中引用非静态方法。
我不太清楚这是什么意思。
我想做的是使用之前设置的首选项初始化变量。
getInt()
不是您在 class SharedPreferences
上调用的方法。相反,您在 SharedPreferences
.
的 实例 上调用它
What I'm trying to do is initialise the variable with the preference that was set before.
检索您的 SharedPreferences
对象,然后对其调用 getInt()
。如果您有保存 SharedPreferences
的代码,您应该已经可以访问该 SharedPreferences
对象,或者至少有检索它的代码。
有关更多信息,请参阅 the documentation on SharedPreferences
。
您无法使用 SharedPreferences.getInt("shufflemode");
以静态方式获取数据
您需要做的是创建一个 SharedPreference
的对象,然后使用 SharedPreference sharedPreference = context.getSharedPreferences("name-of-preference",MODE)
查询它,然后使用 `sharedPreference.getInt("shufflemode")
考虑这个link
private SharedPreferences sharedPref ;
private int mShuffleMode;
in your onCreate
sharedPref= context.getSharedPreferences("preferences_file_key", Context.MODE_PRIVATE);
mShuffleMode = sharedPref.getInt("shufflemode");
private int mShuffleMode = SharedPreferences.getInt("shufflemode");
告诉我不能从静态上下文中引用非静态方法。
我不太清楚这是什么意思。
我想做的是使用之前设置的首选项初始化变量。
getInt()
不是您在 class SharedPreferences
上调用的方法。相反,您在 SharedPreferences
.
What I'm trying to do is initialise the variable with the preference that was set before.
检索您的 SharedPreferences
对象,然后对其调用 getInt()
。如果您有保存 SharedPreferences
的代码,您应该已经可以访问该 SharedPreferences
对象,或者至少有检索它的代码。
有关更多信息,请参阅 the documentation on SharedPreferences
。
您无法使用 SharedPreferences.getInt("shufflemode");
您需要做的是创建一个 SharedPreference
的对象,然后使用 SharedPreference sharedPreference = context.getSharedPreferences("name-of-preference",MODE)
查询它,然后使用 `sharedPreference.getInt("shufflemode")
考虑这个link
private SharedPreferences sharedPref ;
private int mShuffleMode;
in your
onCreate
sharedPref= context.getSharedPreferences("preferences_file_key", Context.MODE_PRIVATE);
mShuffleMode = sharedPref.getInt("shufflemode");