i want to save the settings from my game(bgMusic, sounds and vibration) but while loading the app crashes when starting. The code is:
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);
}
--> the save settings are called in the toggleClickListener It does't make difference if the preferenes and the editor are in the initMethod... Is it maybe then the ToggleButtons are in the Dialog or must I do this in one Method? I hope you can help me. Thanks!
You have to call initDialog
after you initialize the SharedPreference
object, not before. Change
initDialog();
this.preferences = this.getSharedPreferences(GAME_NAME, MODE_PRIVATE);
this.editor = preferences.edit();
to
this.preferences = this.getSharedPreferences(GAME_NAME, MODE_PRIVATE);
this.editor = preferences.edit();
initDialog();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.