0

我使用 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删除了许多设备的菜单按钮,我创建了一个扩展MainActivity的新活动,并放入了保存和加载按钮。

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 保存或加载字符串

4

1 回答 1

0

确保您正确使用按钮侦听器。检查你是否设置了 android:onClick attreibute 之类的

android:onClick="bSave"
于 2013-06-03T14:55:58.687 回答