该函数包含在主Activity中:
public int checkScore(int scoreCurrent) {
int maxscore = PreferenceConnector.readInteger(this, "maxscore", 0);
if (scoreCurrent > maxscore) {
PreferenceConnector.writeInteger(this, "maxscore",
scoreCurrent);
maxscore = scoreCurrent;
}
return maxscore;
}
PreferenceConnector 类简化了 SharedPreferences 的工作。函数 checkScore() 应该在其他类中可用,所以需要做静态。但我有错误:
Cannot use this in a static context
该怎么做以及如何解决?
活动:
public class GameScreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public static int checkScore(int scoreCurrent) {
int maxscore = PreferenceConnector.readInteger(GameScreen.this,
"maxscore", 0);
if (scoreCurrent > maxscore) {
PreferenceConnector.writeInteger(GameScreen.this,
"maxscore", scoreCurrent);
maxscore = scoreCurrent;
}
return maxscore;
}
}