问题是我将设置保存在一个 Activiti
final CheckBox semester=(CheckBox)layout.findViewById(R.id.chbSemeseter);
Context context = getApplicationContext();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor=preferences.edit();
final Calendar calendar=Calendar.getInstance();
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Select current week and semester!");
builder.setPositiveButton("First Week",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (semester.isChecked()){
int week=calendar.get(Calendar.WEEK_OF_YEAR);
editor.putInt(APP_PREFERENCES_SEMESTER,week);
editor.putInt(APP_PREFERENCES_SEMESTER,1);
editor.commit();
} else {
int week=calendar.get(Calendar.WEEK_OF_YEAR);
editor.putInt(APP_PREFERENCES_SEMESTER,week);
editor.putInt(APP_PREFERENCES_SEMESTER,2);
editor.commit();
}
}
});
但是当我尝试在另一个活动中使用它们时,我得到了错误
Context context = getApplicationContext();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences.contains(ActivitySetting.APP_PREFERENCES_WEEK)){
String t=preferences.getInt(ActivitySetting.APP_PREFERENCES_WEEK,0)+"";
MyLog.d("Now week "+t);
}else {
MyLog.d("NOt preferences");
}
错误日志:
//Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Integer
at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:240)
at com.example.Journal.UI.ActivityLiveDiscipline.onCreate
如何在一个 Activity 中保存首选项并在另一个 Activity 中使用?