加载首选项null值

时间:2016-05-13 17:02:23

标签: java android android-studio sharedpreferences

private void SavePreferences(){
    try
    {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        myScore = ((TextView)findViewById(R.id.score)).getText().toString();
        myWord = ((TextView)findViewById(R.id.word)).getText().toString();
        editor.putString("saveScore", myScore.toString());
        editor.putString("saveWord", myWord.toString());
        editor.commit();
    }
    catch (Exception e) {
        System.err.println("scoreText or wordView are null " + e.getMessage());
    }

}    

private void LoadPreferences(){
    try {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        myScore = ((TextView)findViewById(R.id.score)).getText().toString();
        myWord = ((TextView)findViewById(R.id.word)).getText().toString();
        scoreText.setText(sharedPreferences.getString("saveScore", myScore.toString()));
        wordView.setText(sharedPreferences.getString("saveWord", myWord.toString()));
    }
    catch (Exception e) {
        System.err.println("scoreText or wordView are null " + e.getMessage());
    }
}

scoretext和wordView没有被分配来自myScore和myWord的值,尽管在调试时它们都有值。

调试结果:

myScore:"990" scoreText :null
wordView: null myWord"_ _ a _ _"

编辑:

调用savePrefernces的地方:

@Override
public void onBackPressed() {
    SavePreferences();
    super.onBackPressed();
}

调用加载首选项的位置:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_avtivity);
    LoadPreferences();
}

在onBackPressed中调用loadPrefernces来检查myScore或myWord是否为null。

当从onCreate调用load preferences时,正在调用异常!

错误讯息:

第一次调用该消息时,该值仍然为null(预期为异常),并且第二次调用该值时该值不应为null(不期望异常)

  

05-13 17:23:06.671 31547-31547 / mt.edu.mcast.guesstheword2   W / System.err:scoreText或wordView为null尝试调用虚拟   方法' void android.widget.TextView.setText(java.lang.CharSequence)'   在空对象引用上

3 个答案:

答案 0 :(得分:0)

getPreferences”拥有对活动的私人访问权限。如果您使用其他活动中的“LoadPreferences()”,请使用“getSharedPreferences

<强>示例:

SharedPreferences sharedPreferences = getSharedPreferences("YourApp", Context.MODE_PRIVATE);

答案 1 :(得分:0)

将onCreate更改为

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_avtivity);
    initailizeView();
    LoadPreferences();
}

protected void initailizeView() {
    scoreText = (TextView)findViewById(R.id.score)
    wordView = (TextView)findViewById(R.id.word)
}
  

这里我假设R.id.score和R.id.word是两个textViews。

答案 2 :(得分:0)

  private void LoadPreferences(){
        try {
            SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
            initailizeView();
            scoreText.setText(sharedPreferences.getString("saveScore", "default"));
            wordView.setText(sharedPreferences.getString("saveWord","default"));
        }
        catch (Exception e) {
            System.err.println("scoreText or wordView are null " + e.getMessage());
        }
}

sharedPreferences.getString有两个参数&#34; key&#34;和&#34; defValue&#34;。通过重新分配&#34; defValue&#34;中的值。因为我的变量为null,我也会得到一个空值。必须从第一个参数&#34; key&#34;中检索该值。如上所示。