我正在尝试使用Android共享偏好设置来保存某些应用值 这是我的onCreate的代码:
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences preferences = getSharedPreferences("MyPref", MODE_PRIVATE);
boolean firstLaunch = preferences.getBoolean("firstlaunch", true);
System.out.println("FIRST LAUNCH? " + firstLaunch);
if(firstLaunch == true){
SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
editor.putString("language", "en");
editor.putInt("theme", R.style.Default);
editor.putBoolean("firstLaunch", false);
editor.commit();
System.out.println("FIRST LAUNCH:" + preferences.getBoolean("firstLaunch", true));
}
当我重新启动应用时,firstLaunch仍然是真的吗?这是为什么?
答案 0 :(得分:3)
您遇到了个案问题。
firstlaunch
vs firstLaunch
要避免此类问题,您应该使用静态成员。
private static final String KEY_PREFS_NAME = "myPrefs";
private static final String KEY_FIRST_LAUNCH = "firstLaunch";
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences preferences = getSharedPreferences(KEY_PREFS_NAME, MODE_PRIVATE);
boolean firstLaunch = preferences.getBoolean(KEY_FIRST_LAUNCH, true);
System.out.println("FIRST LAUNCH? " + firstLaunch);
if(firstLaunch == true){
SharedPreferences.Editor editor = getSharedPreferences(KEY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("language", "en");
editor.putInt("theme", R.style.Default);
editor.putBoolean(KEY_FIRST_LAUNCH, false);
editor.commit();
System.out.println("FIRST LAUNCH:" + preferences.getBoolean(KEY_FIRST_LAUNCH, true));
}
答案 1 :(得分:3)
检索密钥时,您的密钥值拼写不正确,因此您总是会def bettingRoundHuman():
print "Human, your cards are: " + str(human.card1) + ", " + str(human.card2)
action = raw_input("What is your move? (r/c/f): ")
if action == 'f':
ai.score = ai.score + 1
Table.winner = "Ai"
bettingRound().won = True
return True
elif action == 'r':
raiseAmount = input("Raise by: ")
if bettingRoundHuman().action == 'r':
human.money = human.money - bettingRoundAi().raiseAmount - raiseAmount
Table.pot = Table.pot + bettingRoundAi().raiseAmount + raiseAmount
bettingRound().won = False
return False
elif bettingRoundHuman().action == 'c':
human.money = human.money - raiseAmount
Table.pot = Table.pot + raiseAmount
bettingRound().won = False
return False
else:
human.money = human.money - raiseAmount
Table.pot = Table.pot + raiseAmount
bettingRound().won = False
return False
elif action == 'c':
if bettingRoundAi().action == 'c':
bettingRound().won = False
return True
elif bettingRoundAi().action == 'r':
human.money = human.money - bettingRoundAi().raiseAmount
Table.pot = Table.pot + bettingRoundAi().raiseAmount
bettingRound().won = False
return False
else:
bettingRound().won = False
return False
def bettingRoundAi():
print "Ai, your cards are: " + str(ai.card1) + ", " + str(ai.card2)
action = raw_input("What is your move? (r/c/f): ")
if action == 'f':
human.score = human.score + 1
Table.winner = 'Human'
bettingRound().won = True
return True
elif action == 'r':
raiseAmount = input("Raise by: ")
if bettingRoundHuman().action == 'r':
ai.money = ai.money - bettingRoundHuman().raiseAmount - raiseAmount
Table.pot = Table.pot + bettingRoundHuman().raiseAmount + raiseAmount
bettingRound().won = False
return False
elif bettingRoundHuman().action == 'c':
ai.money = ai.money - raiseAmount
Table.pot = Table.pot + raiseAmount
bettingRound().won = False
return False
elif action == 'c':
if bettingRoundHuman().action == 'r':
ai.money = ai.money - bettingRoundHuman().raiseAmount
Table.pot = Table.pot + bettingRoundHuman().raiseAmount
bettingRound().won = False
return False
elif bettingRoundHuman().action == 'c':
bettingRound().won = False
return True
else:
bettingRound().won = False
return False
def playGame():
startPlayer = bettingRoundOrder()
human.money = human.money - Table.entrymoney
ai.money = ai.money - Table.entrymoney
Table.pot = (Table.pot + 2*Table.entrymoney)
if bettingRound() == False:
print "The table's cards are: " + str(Table.flop1) + ", " + str(Table.flop2) + ", " + str(Table.flop3)
if bettingRound() == False:
print "The table's cards are: " + str(Table.flop1) + ", " + str(Table.flop2) + ", " + str(Table.flop3) + ", " + str(Table.turn)
if bettingRound() == False:
print "The table's cards are: " + str(Table.flop1) + ", " + str(Table.flop2) + ", " + str(Table.flop3) + ", " + str(Table.turn) + ", " + str(Table.river)
if bettingRound() == False:
return cardCompare()
else: return Table.winner
def bettingRoundOrder():
if randint(0,100) >= 50:
return 1
else:
return 2
def bettingRound():
won = False
if playGame().startPlayer == 1:
while flag == False:
finishH = bettingRoundHuman()
finishA = bettingRoundAi()
if finishH == True or finishA == True:
flag = True
else: flag = False
elif playGame().startPlayer == 2:
while flag == False:
finishA = bettingRoundAi()
finishH = bettingRoundHuman()
if finishH == True or finishA == True:
flag = True
else: flag = flase
if won == True: return True
else: return False
进行更改,并且您提交了错误的密钥。
更改
[Keeps repeating and too long to put in question]
File "/home/laurence/Documents/poker (test).py", line 180, in bettingRound
if playGame().startPlayer == 1:
File "/home/laurence/Documents/poker (test).py", line 162, in playGame
if bettingRound() == False:
File "/home/laurence/Documents/poker (test).py", line 180, in bettingRound
if playGame().startPlayer == 1:
File "/home/laurence/Documents/poker (test).py", line 162, in playGame
if bettingRound() == False:
File "/home/laurence/Documents/poker (test).py", line 180, in bettingRound
if playGame().startPlayer == 1:
File "/home/laurence/Documents/poker (test).py", line 157, in playGame
startPlayer = bettingRoundOrder()
File "/home/laurence/Documents/poker (test).py", line 173, in bettingRoundOrder
if randint(0,100) >= 50:
File "/usr/lib/python2.7/random.py", line 242, in randint
return self.randrange(a, b+1)
File "/usr/lib/python2.7/random.py", line 185, in randrange
istart = _int(start)
RuntimeError: maximum recursion depth exceeded while calling a Python object
到
true