作業中の小さなゲームがあり、スコアの更新に取り組んでいますが、正しく動作させることができません。
注:ここに表示するためにプログラムの一部を切り取りました。設定されている他のものがたくさんありますが、それらは「スコア」にまったく触れていないため、コードは少し省略されています。
私のコード:
public class Start_Test extends Activity {
TextView total_points;
long new_total;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
total_points = (TextView) findViewById(R.id.points);
SharedPreferences pref = getSharedPreferences("Prefs",
Context.MODE_PRIVATE);
new_total = pref.getLong("total_points", 0);
setTouchListener();
updateTotal(0);
}
public void updateTotal(long change_in_points) {
SharedPreferences pref = getSharedPreferences("Prefs",
Context.MODE_PRIVATE);
new_total = new_total + change_in_points;
pref.edit().putLong("total_points", new_total);
pref.edit().commit();
total_points.setText("" + new_total);
}
public void setTouchListeners() {
button.setOnTouchListener(new OnTouchListener() {
SharedPreferences pref = getSharedPreferences("Prefs",
Context.MODE_PRIVATE);
@Override
public boolean onTouch(View v, MotionEvent event) {
updateTotal(25);
return false;
}
});
}