EDIT: The problem was not the SharedPreference! It was my wav-data. Android supports only 8- and 16-bit linear PCM wave formats. I used a 32-bit float.
today I recieved a error report via BugSense. A User had a NullPointException because of the sharedPreferences. I recieved this error for the first time.
Here is my relevant code:
The Settings where I set the sharedPreference:
private void dialogSettings() {
final Dialog dialog = new Dialog(this, R.style.dialog_style);
dialog.setContentView(R.layout.dialog_settings);
final CheckBox sound = (CheckBox) dialog.findViewById(R.id.checkBoxSound);
final CheckBox vibration = (CheckBox) dialog.findViewById(R.id.checkBoxVibration);
final SharedPreferences pref = getSharedPreferences("SETTINGS", 0);
sound.setChecked(pref.getBoolean("SOUND", true));
vibration.setChecked(pref.getBoolean("VIBRATION", true));
Button buttonSave = (Button) dialog.findViewById(R.id.buttonSave);
buttonSave.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("SOUND", sound.isChecked());
editor.putBoolean("VIBRATION", vibration.isChecked());
editor.commit();
dialog.dismiss();
}
});
dialog.show();
}
The use of “SOUND” (other Activity):
private boolean SOUND;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_normal_layout);
SharedPreferences pref = getSharedPreferences("SETTINGS", 0);
SOUND = pref.getBoolean("SOUND", true);
....
}
private void evaluateAnswer() {
if(correct) {
if(SOUND) { // LINE 259
ding.seekTo(0);
ding.start();
}
...
}
And here is the Exception:
java.lang.NullPointerException
at mindmApp.quiz.GameNormalActivity.evaluateAnswer(GameNormalActivity.java:259)
So, the variable SOUND is null. But why? Because of SOUND = pref.getBoolean(“SOUND”, true) it must be initialised, or not?
Thanks for all help,
best regards!
SOUND can’t be null (booleans are a primitive type, which can be uninitialized, but not null). The more likely answer is that “ding” is null.