共享偏好Android

时间:2016-03-17 17:34:37

标签: java android sharedpreferences

我试图在SharedPreferences上存储我的应用设置的值,但在第一次运行后,我在尝试打开设置活动时一直收到此错误。

java.lang.RuntimeException: Unable to start activity ComponentInfo{me.leofontes.driversed2/me.leofontes.driversed2.Settings}: android.content.res.Resources$NotFoundException: String resource ID #0x41 

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)

这是我的代码,当我删除SharedPreferences时一切正常。

public class Settings extends AppCompatActivity {
    public static final String PREFS_NAME = "SettingsSharedPrefs";
    public static final String TOTAL_KEY = "total";
    public static final String DAY_KEY = "day";
    public static final String NIGHT_KEY = "night";
    public static final String RESIDENTIAL_KEY = "residential";
    public static final String COMMERCIAL_KEY = "commercial";
    public static final String HIGHWAY_KEY = "highway";
    public static final String CLEAR_KEY = "clear";
    public static final String RAINY_KEY = "rainy";
    public static final String SNOWY_KEY = "snowy";

    EditText totalHours;
    EditText dayHours;
    EditText nightHours;
    EditText resHours;
    EditText comHours;
    EditText hwHours;
    EditText clearHours;
    EditText rainyHours;
    EditText snowyHours;

    private SharedPreferences myPrefs;
    private SharedPreferences.Editor pEditor;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitle(R.string.title_activity_settings);
    toolbar.setNavigationIcon(R.drawable.ic_action_back);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    totalHours = (EditText) findViewById(R.id.totalHoursInput);
    dayHours = (EditText) findViewById(R.id.dayHoursInput);
    nightHours = (EditText) findViewById(R.id.nightHoursInput);
    resHours = (EditText) findViewById(R.id.resHoursInput);
    comHours = (EditText) findViewById(R.id.comHoursInput);
    hwHours = (EditText) findViewById(R.id.hwHoursInput);
    clearHours = (EditText) findViewById(R.id.clearHoursInput);
    rainyHours = (EditText) findViewById(R.id.rainyHoursInput);
    snowyHours = (EditText) findViewById(R.id.snowyHoursInput);


    //Manage Shared Preferences
    Context context = getApplicationContext();  // app level storage
    myPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    pEditor = myPrefs.edit();

    if(myPrefs.getInt(TOTAL_KEY, -1) == -1) {
        pEditor.putInt(TOTAL_KEY, Integer.parseInt(totalHours.getText().toString()));
        pEditor.putInt(DAY_KEY, Integer.parseInt(dayHours.getText().toString()));
        pEditor.putInt(NIGHT_KEY, Integer.parseInt(nightHours.getText().toString()));
        pEditor.putInt(RESIDENTIAL_KEY, Integer.parseInt(resHours.getText().toString()));
        pEditor.putInt(COMMERCIAL_KEY, Integer.parseInt(comHours.getText().toString()));
        pEditor.putInt(HIGHWAY_KEY, Integer.parseInt(hwHours.getText().toString()));
        pEditor.putInt(CLEAR_KEY, Integer.parseInt(clearHours.getText().toString()));
        pEditor.putInt(RAINY_KEY, Integer.parseInt(rainyHours.getText().toString()));
        pEditor.putInt(SNOWY_KEY, Integer.parseInt(snowyHours.getText().toString()));
        pEditor.commit();
    } else {
        totalHours.setText(myPrefs.getInt(TOTAL_KEY, 65));
        dayHours.setText(myPrefs.getInt(DAY_KEY, 55));
        nightHours.setText(myPrefs.getInt(NIGHT_KEY, 10));
        resHours.setText(myPrefs.getInt(RESIDENTIAL_KEY, 4));
        comHours.setText(myPrefs.getInt(COMMERCIAL_KEY, 2));
        hwHours.setText(myPrefs.getInt(HIGHWAY_KEY, 4));
        clearHours.setText(myPrefs.getInt(CLEAR_KEY, 55));
        rainyHours.setText(myPrefs.getInt(RAINY_KEY, 5));
        snowyHours.setText(myPrefs.getInt(SNOWY_KEY, 5));
    }

}

@Override
public void onPause() {

    pEditor.putInt(TOTAL_KEY, Integer.parseInt(totalHours.getText().toString()));
    pEditor.putInt(DAY_KEY, Integer.parseInt(dayHours.getText().toString()));
    pEditor.putInt(NIGHT_KEY, Integer.parseInt(nightHours.getText().toString()));
    pEditor.putInt(RESIDENTIAL_KEY, Integer.parseInt(resHours.getText().toString()));
    pEditor.putInt(COMMERCIAL_KEY, Integer.parseInt(comHours.getText().toString()));
    pEditor.putInt(HIGHWAY_KEY, Integer.parseInt(hwHours.getText().toString()));
    pEditor.putInt(CLEAR_KEY, Integer.parseInt(clearHours.getText().toString()));
    pEditor.putInt(RAINY_KEY, Integer.parseInt(rainyHours.getText().toString()));
    pEditor.putInt(SNOWY_KEY, Integer.parseInt(snowyHours.getText().toString()));

    pEditor.commit();
    super.onPause();
}
}

谢谢!如果需要任何其他信息,请乐意提供!我认为这是微不足道的,但我无法弄清楚这样做的正确方法。

1 个答案:

答案 0 :(得分:1)

好的,所以你遇到的是一个常见的陷阱。当您将值存储在SharedPreferences中时,您将它们显式存储为整数。稍后当你检索它们时,你试图用直接整数值设置文本(几乎是人们会尝试这样做),如下所示:

totalHours.setText(myPrefs.getInt(TOTAL_KEY, 65)); dayHours.setText(myPrefs.getInt(DAY_KEY, 55)); nightHours.setText(myPrefs.getInt(NIGHT_KEY, 10)); resHours.setText(myPrefs.getInt(RESIDENTIAL_KEY, 4)); //...

然而,这里的问题是,当您为setText(int)方法提供int时,它会尝试将文本设置为字符串资源的值,其id与您的值匹配。如果你真的很幸运,应用程序实际上会找到一个字符串资源(虽然有问题)并设置它,但在大多数情况下你会得到你在上面看到的错误。

此问题的解决方案是使用myPrefs.getInt(...)打包您的String.valueOf(...)来电,这会将您的整数转换为字符串,然后在setText(String)上调用TextView方法(避免整个资源shebang)。

希望能够解决你所看到的问题!