2016-03-17 238 views
0

我試圖在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(); 
} 
} 

謝謝!如果需要其他信息,歡迎提供!我認爲這是微不足道的,但我無法弄清楚這樣做的正確方法。

+0

檢查logcat併發布錯誤的行號 –

+0

您需要在其中放置一個字符串,否則它正在尋找具有該'id'中的該id的資源。 – codeMagic

回答

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)); //...

這裏的問題是不過,當您提供帶有int的setText(int)方法時,它會嘗試將文本設置爲字符串資源的值,並使用與您的值匹配的ID。如果你真的很幸運,應用程序實際上會找到一個字符串資源(儘管有問題)並將其設置,但在大多數情況下,您會看到上面顯示的錯誤。

這個問題的解決辦法是用String.valueOf(...)來包裝你myPrefs.getInt(...)通話,這將你的整數轉換爲字符串,然後呼籲TextViewsetText(String)方法(避免整個資源家當)。

希望清除你所看到的問題!

+0

非常感謝!具有很大的意義,我知道這很簡單,但我沒有找到它,謝謝 – leofontes