我試圖在SharedPreferences上存儲我的應用程序的設置值,但在第一次運行後,我在嘗試打開設置活動時不斷收到此錯誤。共享首選項Android
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();
}
}
謝謝!如果需要其他信息,歡迎提供!我認爲這是微不足道的,但我無法弄清楚這樣做的正確方法。
檢查logcat併發布錯誤的行號 –
您需要在其中放置一個字符串,否則它正在尋找具有該'id'中的該id的資源。 – codeMagic