2015-08-30 71 views
-5

因此,在我製作的遊戲中,我使用SharedPreferences來保存我的數據,但它不保存我的數據或恢復它(兩者或兩者)。 (是的,我知道用double.valueOf()得到的是低效率的,但是這不是現在的問題)SharedPreferences不保存我的數據

這裏是我的onCreate(在那裏我得到的所有保存的數據)

 public int hasPlayed = 0; 
public double $money; 
public int $isonmainpage = 1; 
Random random = new Random(); 
public int $cantafford = 0; 
public double $employertimer; 
public double $employercounter; 

public double $employeeupgrade1earnings; 
public double $employeeupgrade1cost = 1; 
public double $employeeupgrade1level; 


public double $employerupgrade1level; 
public double $employerupgrade1earnings; 
public double $employerupgrade1cost = 1; 
public double $allupgrades; 
public String version = "0.0.1"; 
; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    SharedPreferences sharedpreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE); 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    SharedPreferences.Editor editor = sharedpreferences.edit(); 
    hasPlayed = sharedpreferences.getInt("hasPlayed", 1); 
    sharedpreferences.getAll(); 

    if (hasPlayed == 1){ 
     $money = Double.valueOf(sharedpreferences.getString("money", (Double.toString($money)))); 
     $employercounter = Double.valueOf(sharedpreferences.getString("employercounter", (Double.toString($employercounter)))); 
     $employerupgrade1earnings = Double.valueOf(sharedpreferences.getString("employerupgrade1earnings", (Double.toString($employerupgrade1earnings)))); 
     $employerupgrade1level = Double.valueOf(sharedpreferences.getString("employerupgrade1level", (Double.toString($employerupgrade1level)))); 
     $employerupgrade1cost = Double.valueOf(sharedpreferences.getString("employerupgrade1cost", (Double.toString($employerupgrade1cost)))); 
     $employeeupgrade1level = Double.valueOf(sharedpreferences.getString("employeeupgrade1level", (Double.toString($employeeupgrade1level)))); 
     $employeeupgrade1earnings = Double.valueOf(sharedpreferences.getString("employeeupgrade1earnings", (Double.toString($employeeupgrade1earnings)))); 
     $employeeupgrade1cost = Double.valueOf(sharedpreferences.getString("employeeupgrade1cost", (Double.toString($employeeupgrade1cost)))); 
    } 
    if (hasPlayed == 0) { 
     hasPlayed = 1; 
     editor.putInt("hasPlayed", 1); 
     editor.commit(); 
    } 
    updatemoney(); 
    timer(); 

} 

而且這裏是我的onStop /的onPause(當我保存數據)

@Override 
protected void onPause() { 
    super.onPause(); 
    SharedPreferences sharedpreferences = getPreferences(MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedpreferences.edit(); 
    editor.putString("money", (Double.toString($money))); 
    editor.putString("employercounter", (Double.toString($employercounter))); 
    editor.putString("employerupgrade1earnings", (Double.toString($employerupgrade1earnings))); 
    editor.putString("employerupgrade1level", (Double.toString($employerupgrade1level))); 
    editor.putString("employerupgrade1cost", (Double.toString($employerupgrade1cost))); 
    editor.putString("employeeupgrade1level", (Double.toString($employeeupgrade1level))); 
    editor.putString("employeeupgrade1earnings", (Double.toString($employeeupgrade1earnings))); 
    editor.putString("employeeupgrade1cost", (Double.toString($employeeupgrade1cost))); 
    editor.commit(); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    SharedPreferences sharedpreferences = getPreferences(MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedpreferences.edit(); 
    editor.putString("money", (Double.toString($money))); 
    editor.putString("employercounter", (Double.toString($employercounter))); 
    editor.putString("employerupgrade1earnings", (Double.toString($employerupgrade1earnings))); 
    editor.putString("employerupgrade1level", (Double.toString($employerupgrade1level))); 
    editor.putString("employerupgrade1cost", (Double.toString($employerupgrade1cost))); 
    editor.putString("employeeupgrade1level", (Double.toString($employeeupgrade1level))); 
    editor.putString("employeeupgrade1earnings", (Double.toString($employeeupgrade1earnings))); 
    editor.putString("employeeupgrade1cost", (Double.toString($employeeupgrade1cost))); 
    editor.commit(); 
} 

編輯:我永遠不會去了解爲什麼我被低估,如果我不知道爲什麼我被低估。感謝

+1

區分大小寫:'savefile.getInt( 「hasplayed」,0);'不匹配'editor.putInt( 「hasPlayed」,1);' –

+0

他們在上面定義它。沒有包括它來節省空間,對不起 – 343N

+0

問題是你得到sharedprefs同樣的方式來讀寫,這是不正確的,並顯示缺乏研究...然後,... Downvotes來了... –

回答

3

更換

SharedPreferences savefile = getPreferences(MODE_PRIVATE); 

SharedPreferences sharedpreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE); 

,並按照this link

+0

另外'SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);'會做伎倆 –

+0

完成,依然不會保存。 – 343N

+0

完成什麼?你檢查鏈接?或者你等到解決方案來魔術? –

0

我想這應該是因爲這個 hasplayedhasPlayed,關鍵應區分大小寫

savefile.getInt("hasplayed", 0);

editor.putInt("hasPlayed", 1);

+0

我修復了這個問題。但它仍然沒有保存。 – 343N