我有一个弹出窗口内的四个按钮(ImageView),每个按钮将我的服务类中按钮的颜色更改为所需的颜色。当我单击其中一个按钮时,它会改变颜色,但是当我退出应用程序并再次启动时,我的选项不会被保存。Android - 共享首选项不会加载我的选项
//Inner class inside my MainActivity
public static class SetReng {
static int reng;
public SetReng() {
}
public int getReng() {
return this.reng;
}
}
public void onGreen (View view) {
SetReng.reng = Color.GREEN;
ImageView Green = (ImageView)view.findViewById(R.id.kesk);
Green.setPressed(true);
Toast.makeText(MainActivity.this, "Bloq color set to green", Toast.LENGTH_SHORT).show();
editor.putInt("Which", SetReng.reng);
editor.apply();
}
下面是我服务的代码应该加载颜色选项
SetReng putReng = new SetReng();
int theReng= putReng.getReng();
SharedP= PreferenceManager.getDefaultSharedPreferences(context);
int colorOp= SharedP.getInt("Which", theReng);
mButtondeh= new Button(this);
mButtondeh.setBackgroundColor(colorOp);
我在做什么错?
我坚持这最后几天所以任何东西将不胜感激!
你有没有尝试记录或调试? – muratgu
我刚刚尝试过;将值保存到共享首选项时会进行检查,但检索服务类中的值不会。这是什么意思? – Sruda
保存和加载时,您必须使用不同的上下文。 – muratgu