2016-03-17 244 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