我在这里有一个问题,可以将共享首选项设置为Newgame并继续进行最新活动吗? 我有4个活动,menu.class,levelone.class,leveltwo.class,levelthree.class
我将使用我的menu.class
代码
public class menu extends Activity {
public static final String PREF_LEVEL = "Level";
public static final String PREF_IS_SET ="isSet";
public static final String PREF_LIFES = "Lifes";
int level;
Button newgame, continues, continuelocked;
@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);
// Check wether the pref is set or not.
if (pref.getBoolean(PREF_IS_SET, false)) {
// The prefs is set. Do some game logic here. Like checking for level.
if (pref.getInt(PREF_LEVEL,0) == 0) {
// Reset the level
resetPrefs(pref);
}
else {
//Do nothing at all.
}
}
else {
// if it's not set, just create it.
resetPrefs(pref);
}
//i got error on this newgame line
newgame=(Button)findViewById(R.id.buttonnewgame);
newgame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
pref.putInt(PREF_LEVEL,0);
}
});
}
private void resetPrefs(SharedPreferences pref) {
SharedPreferences.Editor editor = pref.edit();
editor.putInt(PREF_LIFES, 6);
editor.putInt(PREF_LEVEL, 0);
editor.putBoolean(PREF_IS_SET,true);
editor.commit();
}
//to saved the current level and if button continue clicked it will open the latest activity, but i dont know the complete code so i use this
public void onResume() {
super.onResume();
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
level = pref.getInt("Level", 0);
if(level == 0)
{
Intent i =new Intent(menu.this, levelone.class);
startActivity(i);
}
if(level == 1)
{
Intent i =new Intent(menu.this, leveltwo.class);
startActivity(i);
}
if(level == 2)
{
Intent i =new Intent(menu.this, levelthree.class);
startActivity(i);
}
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Level", level);
editor.commit();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在我的代码行中有2个错误
newgame=(Button)findViewById(R.id.buttonnewgame);
newgame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
pref.putInt(PREF_LEVEL,0);
}
});
这是, 此行有多个标记
- The method putInt(String, int) is undefined for the type
SharedPreferences
- Cannot refer to the non-final local variable pref defined in an
enclosing scope
- Line breakpoint:aselectmenu [line: 63] - resetPrefs(SharedPreferences)
任何人都可以帮我修复此代码问题吗?
以及如何在点击时设置继续按钮,是否会继续我玩过的最新活动?
答案 0 :(得分:0)
更改此行
myoption1 = myoption1.filter(_.nonEmpty).orElse(myoption2)
到这个
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
然后你需要访问final SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
来实际放置你想要的整数
SharedPreference.Editor