我使用 sharePreference 将字符串保存在 textView 中,之后关闭应用程序并再次打开,它不起作用

I used sharePrereference to save the string in textView, afyer turn off app and open again, it doesnt work

作为标题,我想在编辑 textView 后获取新的字符串,

在我编辑它并再次打开应用程序后,

文本视图仍然是空的,

有人能帮我看看我的代码中遗漏了什么吗,

提前感谢您在此提供的任何帮助。

下面是我的代码:

public class MainActivity extends ActionBarActivity {


    //private SharedPreferences saveUserName;
    private AutoCompleteTextView editText1, editText2, editText3,
                                 editText4, editText5, editText6;
    private ImageButton imageButton1, imageButton2, imageButton3,
                        imageButton4, imageButton5, imageButton6;
    private final String PREFERENCES_NAME = "userinfo"; 


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializeViews();
        onStop();
        getUserName();

        SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);  
        editText1.setText(preferences.getString("EditText1", null));
        editText2.setText(preferences.getString("EditText2", null));
        editText3.setText(preferences.getString("EditText3", null));
        editText4.setText(preferences.getString("EditText4", null)); 
        editText5.setText(preferences.getString("EditText5", null)); 
        editText6.setText(preferences.getString("EditText6", null)); 


    }
    private void initializeViews(){
        editText1 = (AutoCompleteTextView) findViewById(R.id.UserName);
        editText2 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
        editText3 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView3);
        editText4 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView4);
        editText5 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView5);
        editText6 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView6);
    }

    public void onStop() {  
         super.onStop(); 
         SharedPreferences sharePreferences = getSharedPreferences("userName", 0);
         SharedPreferences.Editor editor = sharePreferences.edit();
         editor.putString("EditText1", editText1.getText().toString());
         editor.putString("EditText2", editText2.getText().toString());
         editor.putString("EditText3", editText3.getText().toString());
         editor.putString("EditText4", editText4.getText().toString());
         editor.putString("EditText5", editText5.getText().toString());
         editor.putString("EditText6", editText6.getText().toString());
         editor.commit();
    }

    public void  getUserName()
    {
        SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);  
        editText1.setText(preferences.getString("EditText1", null));
        editText2.setText(preferences.getString("EditText2", null));
        editText3.setText(preferences.getString("EditText3", null));
        editText4.setText(preferences.getString("EditText4", null)); 
        editText5.setText(preferences.getString("EditText5", null)); 
        editText6.setText(preferences.getString("EditText6", null)); 
    }

    public void goToUserInfoActivity(View v)
    {
        Intent it = new Intent(this, UserInfoActivity.class);
        startActivity(it);
    }
}

根据猜测回答。

当您创建一个新的 Activity 时,您将数据放入 SharedPreference。

例如,在初始屏幕中,您首次将数据添加到 SharedPereference。下次进入相同的启动画面并尝试在 SharedPreference 中添加一些值集时,所以下次获取时您的值可能为空或错误。

在您的情况下,您是在 oncreate() 中调用 onStop()。所以该值必须为空。

检查以下内容。你必须修改它。我只是想说明为什么你没有得到任何值。

public class MainActivity extends ActionBarActivity {


    //private SharedPreferences saveUserName;
    private AutoCompleteTextView editText1, editText2, editText3,
                                 editText4, editText5, editText6;
    private ImageButton imageButton1, imageButton2, imageButton3,
                        imageButton4, imageButton5, imageButton6;
    private final String PREFERENCES_NAME = "userinfo"; 


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializeViews();

        getUserName();

        SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);  
        editText1.setText(preferences.getString("EditText1", null));
        editText2.setText(preferences.getString("EditText2", null));
        editText3.setText(preferences.getString("EditText3", null));
        editText4.setText(preferences.getString("EditText4", null)); 
        editText5.setText(preferences.getString("EditText5", null)); 
        editText6.setText(preferences.getString("EditText6", null)); 

onStop();

    }
    private void initializeViews(){
        editText1 = (AutoCompleteTextView) findViewById(R.id.UserName);
        editText2 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
        editText3 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView3);
        editText4 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView4);
        editText5 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView5);
        editText6 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView6);
    }

    public void onStop() {  
         super.onStop(); 
         SharedPreferences sharePreferences = getSharedPreferences("userName", 0);
         SharedPreferences.Editor editor = sharePreferences.edit();
         editor.putString("EditText1", editText1.getText().toString());
         editor.putString("EditText2", editText2.getText().toString());
         editor.putString("EditText3", editText3.getText().toString());
         editor.putString("EditText4", editText4.getText().toString());
         editor.putString("EditText5", editText5.getText().toString());
         editor.putString("EditText6", editText6.getText().toString());
         editor.commit();
    }

    public void  getUserName()
    {
        SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);  
        editText1.setText(preferences.getString("EditText1", null));
        editText2.setText(preferences.getString("EditText2", null));
        editText3.setText(preferences.getString("EditText3", null));
        editText4.setText(preferences.getString("EditText4", null)); 
        editText5.setText(preferences.getString("EditText5", null)); 
        editText6.setText(preferences.getString("EditText6", null)); 
    }

    public void goToUserInfoActivity(View v)
    {
        Intent it = new Intent(this, UserInfoActivity.class);
        startActivity(it);
    }
}

只需创建一个 class Session.java

public class Session {
SharedPreferences pref;
Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "SessionVariable";
public static final String KEY_ID = "id";

public Session(Context context) {
    this._context = context;
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
}

public void createLoginSession(String id) {
    editor.putString(KEY_ID, id);
    editor.commit();
}

public HashMap<String, String> getUserDetails() {
    HashMap<String, String> user = new HashMap<String, String>();
    user.put(KEY_ID, pref.getString(KEY_ID, null));
    return user;
}
}

将值放入 SharedPreference:

Session sm;
String id = ed1.getText().toString();
sm = new Session(MainActivity.this);
sm.createLoginSession(id);

从 SharedPreference 检索:

Session sm;
sm = new Session(Welcome.this);
HashMap<String, String> user = sm.getUserDetails();
String id = user.get(Session.KEY_ID);
ed1.setText("Id = " + id);

我觉得这一行应该是;

SharedPreferences sharePreferences = getSharedPreferences(PREFERENCES_NAME, 0);

而不是;

SharedPreferences sharePreferences = getSharedPreferences("userName", 0);