gpt4 book ai didi

java - Android 应用程序在加载 SharedPreferences 时崩溃 'getBoolean'

转载 作者:行者123 更新时间:2023-11-29 20:56:40 25 4
gpt4 key购买 nike

我想保存我的游戏设置(bgMusic、声音和振动),但在加载应用程序时启动时崩溃。代码是:

private SharedPreferences preferences;
private SharedPreferences.Editor editor;
private boolean bgMusicToggleState, soundToggleState, vibratorToggleState;
...

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...

dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

dialog.setContentView(R.layout.pause_dialog);
dialog.hide();
initDialog();

this.preferences = this.getSharedPreferences(GAME_NAME, MODE_PRIVATE);
this.editor = preferences.edit();
...

}
...
public void saveMusicSettings(){
editor.putBoolean("bgMusicToggleState", bgmusic_toggle.isChecked());
editor.commit();
}
public void saveVibratorSettings(){
editor.putBoolean("vibratorToggleState", vibrateToggle.isChecked());
editor.commit();
}

public void saveSoundSettings(){
editor.putBoolean("soundToggleState", sounds_toggle.isChecked());
editor.commit();
}
...
private void initDialog(){
/// The App crashes here
bgMusicToggleState = preferences.getBoolean("bgMusicToggleState", false);


continue_Button = (Button) dialog.findViewById(R.id.continue_button);
restart_Button = (Button) dialog.findViewById(R.id.restart_button);
exit_Button = (Button) dialog.findViewById(R.id.exit_button);

continue_Button.setOnClickListener(pauseClick);
exit_Button.setOnClickListener(pauseClick);
restart_Button.setOnClickListener(pauseClick);

bgmusic_toggle = (ToggleButton) dialog.findViewById(R.id.backgroundsound_toggle);
bgmusic_toggle.setChecked(bgMusicToggleState);
bgmusic_toggle.setOnClickListener(toggleClickListener);

sounds_toggle = (ToggleButton) dialog.findViewById(R.id.soundtoggle);
sounds_toggle.setChecked(soundToggleState);
sounds_toggle.setOnClickListener(toggleClickListener);

vibrateToggle = (ToggleButton)dialog.findViewById(R.id.vibrate_toggle);
vibrateToggle.setChecked(vibratorToggleState);
vibrateToggle.setOnClickListener(toggleClickListener);


}

--> 保存设置在toggleClickListener中调用如果 preferenes 和编辑器在 initMethod 中,这没有什么区别......是不是 ToggleButtons 在对话框中,或者我必须在一种方法中执行此操作?我希望你可以帮助我。谢谢!

最佳答案

您必须在初始化 SharedPreference 对象之后调用 initDialog,而不是之前。变化

initDialog();
this.preferences = this.getSharedPreferences(GAME_NAME, MODE_PRIVATE);
this.editor = preferences.edit();

this.preferences = this.getSharedPreferences(GAME_NAME, MODE_PRIVATE);
this.editor = preferences.edit();
initDialog();

关于java - Android 应用程序在加载 SharedPreferences 时崩溃 'getBoolean',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27486509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com