我正在開發一個簡單的鬧鐘應用程序,並使用OnSharedPreferenceChangeListener在設置鬧鐘時顯示更改。代碼如下。問題:應用程序崩潰在此行:應用程序崩潰bz獲取默認的共享首選項
//SharedPreferences etPrefs=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
而我找不到原因。奇怪的是,整個事情在大學工作,但當我在家裏使用相同版本的Eclipse打開項目時,它在上述行oqu0崩潰任何建議?
public class NewAlarm extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
private Button btnCancel, btnSave;
private TimePicker tp;
private TextView txtDate;
private DigitalClock dc;
private SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd MMM");
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.new_alarm_prefs);
setContentView(R.layout.new_alarm);
setupRefs();
tp.setIs24HourView(true);
dc.setBackgroundColor(Color.parseColor("#14B909"));
dc.setTextColor(Color.parseColor("#FFFFFF"));
txtDate.setText("Today is: " + sdf.format(new Date()));
}
public void setupRefs() {
btnCancel = (Button) findViewById(R.id.btnCancelNewAlarm);
txtDate = (TextView) findViewById(R.id.txtDate);
tp = (TimePicker) findViewById(R.id.tp);
dc = (DigitalClock) findViewById(R.id.dc);
}
@Override
protected void onResume() {
super.onResume();
getPrefs.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(getPrefs, "Repeat_PREFS");
onSharedPreferenceChanged(getPrefs, "et_PREFS");
btnCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
btnSave = (Button) findViewById(R.id.btnSaveNewAlarm);
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
getPrefs.unregisterOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences getPrefs, String key) {
Preference pref = findPreference(key);
if (pref instanceof ListPreference) {
ListPreference listPref = (ListPreference) pref;
pref.setSummary(listPref.getEntry().toString());
}
if (pref instanceof EditTextPreference) {
EditTextPreference etPref = (EditTextPreference) pref;
pref.setSummary(etPref.getText());
}
}
}
發表您的logcat –