我有一个主应用程序,我想使用SharedPreferences存储分数整数

时间:2018-11-15 11:28:53

标签: android

重新启动应用程序时,它将返回默认值,而不是  以前保存的值。我需要一些帮助。 这里的主要问题是我遵循了sharedpreferences要求的步骤,该步骤已保存,但无法在重新打开应用程序时恢复。

重新打开应用程序时返回的默认值,即
Score=myScore.getInt ( "score",0 );

这是分数的样本 应用代码。

package tips.admin.tipsscore.score;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
  TextView score;
Button add,sub;
Context context;
int Score=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        score=(TextView)findViewById ( R.id.score );
        add=(Button)findViewById ( R.id.increase );
        sub=(Button)findViewById ( R.id.decrease );
        SharedPreferences myScore=context.getSharedPreferences ("myscore", Context.MODE_PRIVATE);
        Score=myScore.getInt ( "score",0 );
        score.setText ( "SCORE :  "+Score );
        add.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View v) {
                Score+=45;
                SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor= sharedPreferences.edit ();
                editor.putInt ( "SCORE :  ",Score);
                editor.commit ();
                score.setText ( "SCORE :  "+Score );

            }
        } );
        sub.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View v) {
                Score-=21;
                SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor= sharedPreferences.edit ();
                editor.putInt ( "SCORE :   ",Score);
                editor.commit ();
                score.setText ( "SCORE :  "+Score );
           }
       } );
   }

}

2 个答案:

答案 0 :(得分:1)

您使用其他键从共享的首选项中保存和还原值。 您需要使用相同的密钥。

例如:

保存:

Failed authorization procedure. cdn.example.com (http-01): urn:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://cdn.example.com/.well-known/acme-challenge/PaKenmvAqHoOdOBUhThxxxxx: "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p" IMPORTANT NOTES: - The following errors were reported by the server: Domain: cdn.example.com Type: unauthorized Detail: Invalid response from http://cdn.example.com/.well-known/acme-challenge/PaKenmvAqHoOdOBUhThxxxxx: "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p" To fix these errors, please make sure that your domain name was entered correctly and the DNS A/AAAA record(s) for that domain contain(s) the right IP address. ;

阅读:

editor.putInt ("score_value", Score)

答案 1 :(得分:1)

请改用字符串常量,以避免拼写错误的标签。

在您的课程中声明:

private static final String NAME_OF_TAG = “name_of_tag”

然后使用该字符串代替:

editor.putInt(NAME_OF_TAG, 3);

int value = preferences.getInt(NAME_OF_TAG, 0);