Java SharedReferences在尝试检索时使应用程序崩溃

Java SharedReferences在尝试检索时使应用程序崩溃,java,android,sharedpreferences,Java,Android,Sharedpreferences,我对安卓非常陌生,也习惯了iOS。与SharedPreferences有点困难。我基本上是第一次在我的设置视图中初始化,高分为0 现在在另一个游戏视图中,当我试图检索此分数并将其与当前分数进行比较以更新它时,它崩溃了。一旦它试图读取它,应用程序就会崩溃 这将在设置页面中初始化: public void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub // similar to

我对安卓非常陌生,也习惯了iOS。与SharedPreferences有点困难。我基本上是第一次在我的设置视图中初始化,高分为0

现在在另一个游戏视图中,当我试图检索此分数并将其与当前分数进行比较以更新它时,它崩溃了。一旦它试图读取它,应用程序就会崩溃

这将在设置页面中初始化:

public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub

    // similar to view did appear
    System.out.println("Settings viewdidappear");
    super.onWindowFocusChanged(hasFocus);

    int firstTime = 10;
    SharedPreferences preferences = this.getSharedPreferences("Settings", Context.MODE_PRIVATE);

    int getVar = preferences.getInt("CheckVar", 0);

    // set the value first time if not existing already
    if (getVar != 10) {

        System.out.println("Settings not existant: " + getVar);
        SharedPreferences.Editor editor = preferences.edit();
        getVar = 10;
        editor.putInt("CheckVar", getVar);
        editor.putInt("HighScore", 0);
        editor.commit();

        // add loading for save score


    } else {
        System.out.println("Settings Already Exists: " + getVar);
    }

}
这在游戏页面中:

public class GameLogic extends View {

    SharedPreferences settings;

    public void save(Context context) {
        System.out.println("Running save");

        settings = context.getSharedPreferences("Settings", Context.MODE_PRIVATE);
        int getVar = settings.getInt("HighScore", 0);
        if (getVar > total_score) {
            System.out.println("Putting new value " + total_score);
            SharedPreferences.Editor editor = settings.edit();
            getVar = total_score;
            editor.putInt("HighScore", getVar);
            editor.commit();
        } else {
            System.out.println("Previous score is higher " + getVar);
        }

    }


    public void DidILose() {
        score();

        if (TappedMine == 2) {

            save(null);

            flag_for_loss = 1;
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    getContext());
            alertDialogBuilder.setTitle("Game Over");
            alertDialogBuilder
                    .setMessage("Play again!  \nScore: " + total_score + "\nTime: " + 
                            time_done + " seconds" + "\n Unflagged Mines:" + unflagged_mines)
                    .setCancelable(false)
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int id) {
                                    ResetTheGame();
                                }
                            }
                    );
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }
    }

}
调用
save(空)

并尝试使用参数访问对象,即
null
,导致
NullPointerException
,使应用程序崩溃

 public  void save(Context context) {
        System.out.println("Running save");
        settings = context.getSharedPreferences("Settings", Context.MODE_PRIVATE);  

如果(getVar!=10)
为非真,则
SharedReferences
中不存在具有
HighScore
名称的键,因此请使用
设置。包含(“HighScore”)
在检索值之前,我无法确定应该传递什么作为参数。请您建议
查看
,使用
getContext()
方法。使用itI我尝试过不使用save方法,但它不允许我这样做。我在某个地方读到,我需要使用上下文对象