为什么我的 "SharedPreferences" 不起作用?

Why doesn't my "SharedPreferences" work?

问题是,"Wizard2Activity" 中的文本字段只显示 "nothing" 而不是用户在 "Wizard1" 中的 "EditText" 中键入的内容。

Wizard1.java:

package com.CENSORED.CENSORED;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Wizard1 extends Activity {

public static final String PREFS ="examplePrefs";


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


final EditText et = (EditText)findViewById(R.id.editText1);


Button nextAct = (Button)findViewById(R.id.button1);

nextAct.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v){

    String message = et.getText().toString();   

    SharedPreferences examplePrefs = getSharedPreferences(PREFS, 0);
    Editor editor = examplePrefs.edit();
    editor.putString("usermessage", message);
    editor.commit();

    Intent i = new Intent(getApplicationContext(),         
  Wizard2Activity.class);
    startActivity(i);

}


});


}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is   

    present.
    getMenuInflater().inflate(R.menu.wizard1, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);



}


}

和精灵 2 Activity:

package com.CENSORED.CENSORED;

import android.app.Activity; 
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Wizard2Activity extends Activity {

public static final String PREFS ="examplePrefs";






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


    TextView tv = (TextView)findViewById(R.id.textView1);

    SharedPreferences example = getSharedPreferences(PREFS, 0);

    String userString = example.getString("userMessage", "nothing");

    tv.setText(userString);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is   
present.
    getMenuInflater().inflate(R.menu.wizard2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

感谢您的帮助! :)

因为方法

editor.putString("usermessage", message);

example.getString("userMessage", "nothing");

有不同的密钥。

你应该改变

String userString = example.getString("userMessage", "nothing");

String userString = example.getString("usermessage", "nothing");

关键是usermessage。打错了。