我的项目中的共享偏好有一些问题,即共享偏好没有从另一个活动接收到int值
有两个类:menu.class
和levelone.class
我在menu.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Lifes", 6);
editor.putInt("Hints", 6);
editor.putInt("Level", 1);
editor.commit();
f1=(Button)findViewById(R.id.f1);
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
f1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent i =new Intent(menu.this, levelone.class);
startActivity(i);
}
});
f2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent i =new Intent(menu.this, leveltwo.class);
startActivity(i);
}
});
f3=(Button)findViewById(R.id.f3);
f3lock=(ImageView)findViewById(R.id.f3lock);
//this the code to receive int value from another activity
levelunlocked = pref.getInt("Level", 0);
if(levelunlocked == 2)
{
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
if(levelunlocked == 3)
{
f3.setVisibility(View.VISIBLE);
f3lock.setVisibility(View.GONE);
}
}
我在levelone.class
中使用此代码来获取menu.class
int gamelifes, gamehints, gamelevel, index=0;
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
gamelifes = pref.getInt("Lifes", 0);
gamehints = pref.getInt("Hints", 0);
gamelevel = pref.getInt("Level", 0);
//the value from sharedpreferences is use to be a text by use code below
lifes1 =(TextView)findViewById(R.id.lifestext1);
lifes1.setTextColor(Color.RED);
lifes1.setText(String.valueOf(gamelifes));
hints1 =(TextView)findViewById(R.id.hintstext1);
hints1.setTextColor(Color.GRAY);
hints1.setText(String.valueOf(gamehints));
并使用新数据保存共享偏好
String answer=edittextanswer1.getText().toString();
if(answer.equalsIgnoreCase(answer1[index]))
{
gamelevel++;
image.setVisibility(View.GONE);
finishbutton.setVisibility(View.VISIBLE);
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Lifes", gamelifes);
editor.putInt("Hints", gamehints);
editor.putInt("Level", gamelevel);
editor.commit();
else
{
tryagain1.setVisibility(View.VISIBLE);
gamelifes--;
lifes1.setText(String.valueOf(gamelifes));
}
然后,如果单击完成按钮,它将是这样的
finishbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
finish();
}
});
所以levelone.class
已完成并返回menu.class
但是sharedpreferences检索的值代码不起作用
//this the code to receive int value from another activity
levelunlocked = pref.getInt("Level", 0);
if(levelunlocked == 2)
{
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
if(levelunlocked == 3)
{
f3.setVisibility(View.VISIBLE);
f3lock.setVisibility(View.GONE);
}
}
任何人都可以给出一些解决方案吗?
答案 0 :(得分:1)
您已在onCreate()
中添加了检索代码。当您从menu
参加活动levalone
时,系统不会调用此方法。
在onResume()
//this the code to receive int value from another activity
levelunlocked = pref.getInt("Level", 0);
if(levelunlocked == 2)
{
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
if(levelunlocked == 3)
{
f3.setVisibility(View.VISIBLE);
f3lock.setVisibility(View.GONE);
}