私の新しいアプリでは、ボタンをクリックするとカウンタが増えます。私はsharedPreferencesでハイスコアを保存したいので、次回のアプリ起動時にスコアが保存されて表示されます。問題は、他の答えられた質問でさえ、私が実際にそれを働かせることはないということです。android - sharedPreferencesでintを保存する
package com.example.test;
public class MainActivity extends ActionBarActivity {
public int score = 0;
public int highscore = 0;
TextView tvscore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvhighscore= (TextView) findViewById(R.id.highscore);
tvscore = (TextView) findViewById(R.id.score);
Button count = (Button) findViewById(R.id.button1);
tvhighscore.setText(String.valueOf(highscore));
SharedPreferences prefs = this.getSharedPreferences("score", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt("score", 0);
editor.commit();
}
public void onClick (View view) {
score++;
tvscore.setText(String.valueOf(score));
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int highscore = prefs.getInt("score", 0);
}
}
ボタンをクリックすると 'SharedPreferences'にスコアを保存する必要があります。 –
@ρяσѕρєяKが正しいです。さらに、共有設定ファイルに同じ名前を使用していることを確認してください。 –
私は '' score ''と同じ名前を使用していませんか? –