应用程序关闭后如何保存Highscore?

时间:2017-04-29 19:02:21

标签: java android android-sharedpreferences

我想保存高分,即使在App关闭后,也会保存我点击图像按钮多少次。我希望在应用程序运行的整个过程中看到高分 但是当我尝试我的代码时,App每次都崩溃了,我不知道我犯了什么错误。

编辑:现在应用程序没有开始,但是Highscore总是零! 编辑2:现在搞定了!这是适用于我的代码。

 public class MainActivity extends AppCompatActivity {
private ImageView cirlce;
private TextView Zahl;
private ImageButton reset;
int counter = 0;
int readHighscore;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cirlce = (ImageView) findViewById(R.id.imageView);
    Zahl = (TextView) findViewById(R.id.Zahl);
    Zahl.setText(Integer.toString(counter));
    TextView highscore = (TextView) findViewById(R.id.highscore);
    highscore.setText(Integer.toString(readHighscore));

 cirlce.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            counter++;
            Zahl.setText(Integer.toString(counter));



   cirlce.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            counter++;
            Zahl.setText(Integer.toString(counter));
            TextView highscore = (TextView) findViewById(R.id.highscore);
            highscore.setText(Integer.toString(readHighscore));

            if (readHighscore > counter) {
                highscore.setText(Integer.toString(readHighscore));

            }
            else {
                readHighscore = counter;
                highscore.setText(Integer.toString(counter));

            }

SharedPreferences prefs = this.getSharedPreferences(“myPrefsKey”,Context.MODE_PRIVATE);         readHighscore = prefs.getInt(“highscore”,0);

    if (readHighscore > counter) {
        highscore.setText(Integer.toString(readHighscore));

    }
    else {
        readHighscore = counter;
        highscore.setText(Integer.toString(counter));

    }
}


@Override
protected void onPause() {
    super.onPause();

    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt("highscore", readHighscore);

    editor.commit();

}

}

这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/x"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="348dp"
        android:layout_height="346dp"
        app:srcCompat="@drawable/cirlce"
        tools:layout_editor_absoluteX="18dp"
        tools:layout_editor_absoluteY="82dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/Zahl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@null"
        android:textSize="100sp"
        android:typeface="sans"
        android:layout_centerInParent="true"/>


    <TextView
        android:id="@+id/highscore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp"
        android:text="@string/Highscore" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您应该为高分值添加一个键

  

editor.putInt(“”,readHighscore);

更改为

  

editor.putInt(“Score”,readHighscore);