保存并加载字符串

时间:2013-06-03 14:41:59

标签: android

我使用了Menu onOptionsItemSelect(MENU)来保存和加载这样的字符串

public  static String filename = "MySharedString";
    SharedPreferences someData;
String s;
 someData = getSharedPreferences(filename, 0);

case R.id.save:
            Toast.makeText(getApplicationContext(), "Samples saved", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor editor1 = someData.edit();
            editor1.putString("ourString1", s); 

并像这样加载

case R.id.load:

    s = someData.getString("ourString1", "Couldn't Load Data");

它工作得很好......因为android取消了很多设备的MENU按钮我做了一个新的活动,扩展了MainActivity,我输入了Save and Load按钮。

public void bSave (View v){
        SharedPreferences.Editor editor1 = someData.edit();
        editor1.putString("ourString1", s);
        editor1.commit();

并加载

public void bLoad (View v){
        s = someData.getString("ourString1", "Couldn't Load Data");

由于某种原因,它不起作用,我重复我做了一个新的活动(public class Menu extends MainActivity{),我开始作为Intent,它不会保存或加载来自MainActivity的字符串

1 个答案:

答案 0 :(得分:0)

确保正确使用按钮侦听器。检查你是否设置了android:onClick attreibute,如

android:onClick="bSave"