我使用sharedpreferences保存了几个变量,但我的保存方法使用空指针异常使应用程序崩溃。
public void save() {
SharedPreferences sp = context.getSharedPreferences(saveFile, Context.MODE_PRIVATE);
//Use the editor for easier management, no calling edit and commit so many times.
SharedPreferences.Editor editor = sp.edit();
我想重写SharedPreferences的小扩展函数。现在它看起来像这样
fun SharedPreferences.put(arg: Pair<String, Any>) {
val e = edit()
val s = arg.second
when (s) {
is Int -> e.putInt(arg.first, s)
is String -> e.putString(arg.first, s)
is Boolean -> e.putBoolean(arg.first, s)
我刚开始使用共享首选项,在我第一次尝试的时候,我得到了一些对我来说没有意义的错误。我像这样赋值:
int saveScore = sp.getInt("SAVE_SPOT",0); //This is intentional to get the
//default value of 0 to go to case 0
switch(saveScore){
case 0:
SharedPreferences.Editor edit1 = sp.edit();
我想在SharedPreferences中保存两个整数,但它们不会被保存。我有一个最小值和最大值的RangeSlider。滑块值肯定会被更新,但是当我保存它们并调用sharedPreferences.getInt(.)时,这些值不会被保存。共享首选项仍然具有默认值。有人看到我的错误了吗?
提前感谢!
public class SettingsFragment extends Fragment {
private FragmentSettingsBinding binding;
private SettingsViewModel settingsViewModel;
p
我想用SwithCompat动态改变我的应用程序的主题,所以我实现了这个: public class SettingsActivity extends AppCompatActivity {
private final static int THEME_LIGHT = 2;
private final static int THEME_DARK = 1;
@BindView(R.id.summary)
TextView summary;
@BindView(R.id.themeModeSwitchCompat)
SwitchCompat t
我在DialogFragment中使用DialogFragment时遇到了一些困难。我一直把NullPointerException放在sharedPreferences = getActivity().getSharedPreferences("pref", 0);的线上。这是五月的基本代码。
public class ADialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
SharedPreferences sharedPreferences;
p
我有一个SharedPreferences,它在应用程序中每执行一次操作后激活一次interstitial。我希望SharedPreferences在用户离开应用程序时清除这些设置,以便可以在每次应用程序启动后加载interstitial。请帮帮忙。
SharedPreferences admob = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = admob.edit();
// Save counter value back
我正在制作一个带有计数器的应用程序,当我点击一个ImageView时,它就会上升。这是可行的,但现在我想将计数器保存到SharedPreferences对象中。
我尝试在onCreate方法中初始化SP对象,这显然不起作用。当我尝试使用sp.getInt(.)在全球范围内,它也不起作用。我理解为什么会出现问题(在sp对象初始化之前尝试从sp对象获取Integer ),但我不知道如何解决它。
public class MainActivity extends AppCompatActivity {
int counterint;
TextView counter;
St
我在许多类似的SharedPreferences问题中搜索过,但不能应用到我的项目中。它是一个RuntimeException,它需要初始化一个SharedPreferences延迟变量。
错误信息:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.tabuild3/com.me.tabuild3.Tutorial}: kotlin.UninitializedPropertyAccessException: lateinit property mPref has not been initial
我有一个使用sharedpreferences保存一些数据的活动,如下所示:
public void birthDateSharedPreferences(int calculatedBirthYear, int calculatedBirthMonth, int calculatedBirthDay)
{
SharedPreferences birthDatePreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = b
我正在尝试使用两种SharedPreferences,第一种是有效的,而第二种则不是
这是我的Java代码
public class JogoActivity extends AppCompatActivity implements View.OnClickListener {
//OTHERS VARIABLES
public TextView txtViewResult,txtCoins, txtTentativas;
private static final String PREF_NAME = "JogoActivityPreferences";
int resu
//Activity-- mTweetApp.SetCaseInfo(type,teet); //storing in another class
public void SetCaseInfo(String PatientType,ArrayList arr){
// All objects are from
SharedPreferences settings =setSharedPreferences(DEALSPOTR_PREFS,0);
SharedPreferences.Editor editor = settings.edit();
editor.pu