0

问题是我将设置保存在一个 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 中使用?

4

2 回答 2

0

似乎您设置了 APP_PREFERENCES_SEMESTER 设置,但您阅读了 APP_PREFERENCES_WEEK 设置。您应该使用相同的名称。

于 2013-05-02T08:35:27.313 回答
0

对不同的整数值使用相同的键,因此请制作不同的键,如下所示

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,week);
                                editor.putInt(APP_PREFERENCES_SEMESTER,1);
                                editor.commit();
                            } else {
                                int week=calendar.get(Calendar.WEEK_OF_YEAR);
                                editor.putInt(APP_PREFERENCES_SEMESTER_WEEK,week);
                                editor.putInt(APP_PREFERENCES_SEMESTER,2);
                                editor.commit();
                                }
                        }
                    });

在另一个活动中使用:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
int intweek = preferences.getInt(APP_PREFERENCES_SEMESTER_WEEK, 0);
int intextra = preferences.getInt(APP_PREFERENCES_SEMESTER, 0);

还有一件事不需要这样做:

if (preferences.contains(ActivitySetting.APP_PREFERENCES_WEEK))

因为如果没有设置 Prefrance,那么它会给你 0。

于 2013-05-02T08:36:47.717 回答