这个问题在这里被抛出很多。然而,我的实施方面是错误的,所以如果你们可以帮助我,这将有所帮助。提前致谢。对不起,如果这个问题很像诺布。
我开发Court Counter, which can be found here.我最近开始为我的应用添加保存支持。但是,我不确定我是否正确地做到了。所有其他堆栈溢出主题都提到了编辑器,但是Android Studio会将我更正为SharedPreferences.Editor。我假设这个改变了Marshmallow或其他什么。
无论如何,我添加了这些代码行来保存和加载:
/**
* Saves your current session
* SharedPreferences key = courtCounter
* team A key = teamA
* team B key = teamB
*/
public void saveSession(View v) {
//We will get saved preferences!
SharedPreferences prefs = this.getSharedPreferences("courtCounter", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("teamA",scoreA);
editor.putInt("teamB",scoreB);
editor.commit();
}
/**
* Loads previous session
* The config is same as saveSession
*/
public void loadSession(View v) {
//We will get the preferences!
SharedPreferences prefs = this.getSharedPreferences("courtCounter", Context.MODE_PRIVATE);
scoreA = prefs.getInt("teamA", 0);
scoreB = prefs.getInt("teamB", 0);
}
在分数更新后调用saveSession,您可以在下面看到:
/**
* New way of adding score
*
* @param score Score that you need to add to the team
* @param team Team that you want the score added to
* @param last Last clicked item, for example, a1. This will be used for the undo action.
*/
public void scoreAdd(int score, char team, String last) {
Button undo = (Button) findViewById(R.id.undo_button);
undo.setEnabled(true);
lastClicked = last;
if (team == 'a') {
scoreA += score;
displayScore(scoreA, 'a');
}
if (team == 'b') {
scoreB += score;
displayScore(scoreB, 'b');
}
saveSession();
}
然而,这会引发错误,说我没有提供(视图)或括号中的任何内容。我怎样才能解决这个问题?我不需要任何输入处理程序,但如果我没有制作一个输入处理器,Android Studio就会吓坏。
现在,代码在这里工作正常:
public void resetAll(View view) {
Button undo = (Button) findViewById(R.id.undo_button);
undo.setEnabled(false);
lastClicked = "reset";
scoreA = 0;
scoreB = 0;
displayScore(scoreA, 'a');
displayScore(scoreB, 'b');
saveSession(view);
}
我假设这是因为resetAll有一个视图输入参数。但是,从onClick调用resetAll。
代码不起作用的其他地方包括onCreate。
有什么想法吗?
答案 0 :(得分:1)
您的saveSession甚至不使用方法中的View v,为什么要将其作为参数?删除它。
var callDuration : Int?
你有很多不必要的参数,因为你的方法甚至不使用它,所以没有必要。