【发布时间】:2012-09-06 20:42:14
【问题描述】:
编辑:问题不在于 SharedPreference!这是我的 wav 数据。 Android 仅支持 8 位和 16 位线性 PCM 波形格式。我使用了 32 位浮点数。
今天我通过 BugSense 收到了错误报告。由于 sharedPreferences,用户有 NullPointException。我第一次收到这个错误。
这是我的相关代码:
我设置 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();
}
“SOUND”(其他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();
}
...
}
这里是例外:
java.lang.NullPointerException
at mindmApp.quiz.GameNormalActivity.evaluateAnswer(GameNormalActivity.java:259)
因此,变量 SOUND 为空。但为什么?因为 SOUND = pref.getBoolean("SOUND", true) 它必须初始化,还是不初始化?
感谢大家的帮助,
最好的问候!
【问题讨论】:
-
SOUND 是原始布尔值,因此不能为空。可能行数有误,真正的原因是 ding 为空。
-
我想我找到了答案。 Android 仅支持 8 位和 16 位线性 PCM 波形格式。我使用了 32 位浮点数。很抱歉给您带来麻烦,仍然感谢!最好的问候!
标签: android nullpointerexception sharedpreferences