Thursday, May 2, 2013

[android help] Save preferences in one Activity and use in another


The problem is that I save the settings in one 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();
}
}
});


But when I tried use them in another activity I get error



Context context = getApplicationContext();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences.contains(ActivitySetting.APP_PREFERENCES_WEEK)){
String t=preferences.getInt(ActivitySetting.APP_PREFERENCES_WEEK,0)+"";
//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

MyLog.d("Now week "+t);
}else {MyLog.d("NOt preferences");}


How can I save preferences in one Activity and use in another?



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...