为什么我的“SharedPreferences”不起作用?

时间:2022-08-23 12:11:27

The Problem is, that the Text field which is in "Wizard2Activity" just shows "nothing" and not what the user has typed in the "EditText" in "Wizard1".

问题是,“Wizard2Activity”中的Text字段只显示“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);



}


}

And Wizard 2 Activity:

和精灵2活动:

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);
}
}

Thank you for your help! :)

谢谢您的帮助! :)

2 个解决方案

#1


You should change

你应该改变

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

to

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

key is usermessage. It's just typo.

关键是usermessage。这只是错字。

#2


Because the method

因为方法

editor.putString("usermessage", message);

and

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

have different key.

有不同的关键。

#1


You should change

你应该改变

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

to

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

key is usermessage. It's just typo.

关键是usermessage。这只是错字。

#2


Because the method

因为方法

editor.putString("usermessage", message);

and

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

have different key.

有不同的关键。