0

(删除了帖子的旧内容)

编辑#2:好的,所以现在我很清楚是编辑器试图达到导致空指针异常的首选项。关于如何解决它的任何帮助?

这是更新的活动:

     public SharedPreferences sharedPreferences;
 Editor editor;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // requesting to turn the title OFF
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // making it full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // set our MainGamePanel as the View
    setContentView(new MainGamePanel(this));

 // Restore preferences

        this.sharedPreferences = getPreferences(MODE_PRIVATE);
        this.editor = sharedPreferences.edit();

        try {
        int wins = GetPreferences("wins");
        int fails = GetPreferences("fails");
        gamePanel.winn = wins;
        gamePanel.failn = fails;
        } catch (NullPointerException npe) {
            Log.d(TAG, "Nothing to load");
        }

    //INIT SOUND
    mSoundManager.initSounds(getBaseContext());
    //SOUNDS
    mSoundManager.addSound(1, R.raw.draw);
    mSoundManager.addSound(2, R.raw.cheer);
    mSoundManager.addSound(3, R.raw.boo);

}

@SuppressWarnings("deprecation")
@Override
public void onBackPressed() 
{
    super.onBackPressed();
    if (gamePanel.gamei==true) {
        gamePanel.back();
    } else if (gamePanel.menui==true) {
        finish();
        System.runFinalizersOnExit(true);
        System.exit(0);
    }
}

public void onPause() 
{
    super.onPause();
    //KILL ALL
    finish();
    System.runFinalizersOnExit(true);
    System.exit(0);
}

@Override
protected void onStop(){
   super.onStop();
  //KILL ALL
  finish();
  System.runFinalizersOnExit(true);
  System.exit(0);
}

public int GetPreferences(String key) {
    return sharedPreferences.getInt(key, 0);
}

public void SavePreferences(String key, int value) {
editor.putInt(key, value);    
editor.apply();  
} 

public void writeWin () { 
    SavePreferences("wins", gamePanel.winn);
}
public void writeFail () { 
    SavePreferences("fails", gamePanel.failn);
}

编辑器是导致 nullpointerexception: 的原因this.editor = sharedPreferences.edit();编辑:它是导致 nullpointerexception 的 sharedPreferences,而不是编辑器。

编辑器似乎无法达到 Preference: this.sharedPreferences = getPreferences(MODE_PRIVATE);

关于如何解决这个问题的任何想法?

4

2 回答 2

0

我终于通过使用监听器解决了它!它显然是在活动内部为空的游戏面板。转到此处了解更多信息: 尝试访问活动时出现 nullpointerexception

于 2012-11-18T11:34:53.210 回答
0

它看起来像声明

编辑编辑;

应该

SharedPreferences.Editor 编辑器;

于 2012-11-16T21:56:23.263 回答