2017-01-12 1 views

답변

3

사례에 문제가 있습니다. firstlaunch vs firstLaunch

이러한 문제를 방지하려면 정적 멤버를 사용해야합니다. 당신이 그것을 검색 할 때 당신은 항상 true 변화를 얻을 잘못된 키에 투입 있도록

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)); 
} 
+0

와우로 변경

boolean firstLaunch = preferences.getBoolean("firstlaunch", true); 

그 오류라고 생각하기! 고마워. 나는 이런 종류의 오류를 피하기 위해 앞으로 이것을 할 것이다. –

3

키 값을 제대로 철자가되지 않습니다.

boolean firstLaunch = preferences.getBoolean("firstLaunch", true); 
관련 문제