(删除了邮件的旧内容)
编辑#2:好的,现在我很清楚,编辑试图达到导致nullpointerexception的偏好。有关如何修复它的任何帮助吗?以下是更新的活动:
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,而不是编辑器。
似乎编辑无法访问偏好:this.sharedPreferences = getPreferences(MODE_PRIVATE);
。
有关如何解决这个问题的想法吗?
答案 0 :(得分:0)
看起来像声明
编辑编辑;
应该是
SharedPreferences.Editor编辑器;
答案 1 :(得分:0)
我终于通过使用听众来修复它了!它显然是在活动中无效的gamePanel。点击这里了解更多信息: nullpointerexception when trying to reach activity