【问题标题】:Sharedpreferences not saving or loading dataSharedpreferences 不保存或加载数据
【发布时间】:2013-08-01 19:17:15
【问题描述】:

我有这段代码,我试图在“InitialPreferences”活动中使用共享首选项保存一些持久性数据,然后“StartScreen”活动在应用程序下次启动时搜索保存的首选项,并将用户定向到“InitialPreferences”或“MainActivity”。 我的问题是数据没有保存,我看不到问题,谁能指导我解决问题并告诉我我做错了什么?

谢谢

StartScreen 活动:

 public class StartScreen extends Activity {

CountDownTimer waitTimer;
public static final String APP_PREFERENCES = "AppPrefs";
SharedPreferences settings; 
SharedPreferences.Editor prefEditor;

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

     settings = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);
     //    prefEditor = settings.edit();

     waitTimer = new CountDownTimer(5000, 300) {

       public void onTick(long millisUntilFinished) {
          //called every 300 milliseconds, which could be used to
          //send messages or some other action
       }
       public void onFinish() {
          //After 5000 milliseconds (5 sec) finish current 
          //if you would like to execute something when time finishes 
           getPreferences();
       }
     }.start();
}   

private void getPreferences() {
    // TODO Auto-generated method stub
    String UserName = settings.getString("UserName", "");

    if (UserName != null) {
        // the key does not exist
                Intent intent=new           Intent(StartScreen.this,InitialPreferences.class);
                startActivity(intent);
            }
    if (UserName.equals("UserName")){
        // handle the value
                Intent intent=new Intent(StartScreen.this,MainActivity.class);
                startActivity(intent);
     }       
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.start_screen, menu);
    return true;
}
}

InitialPreferences 活动:

  public class InitialPreferences extends Activity implements OnClickListener {

EditText editText1;
Button button1;
TextView textView1;
RadioGroup radioGroup;
RadioButton radioPosistionButton;   

public static final String APP_PREFERENCES = "AppPrefs";
SharedPreferences settings; 
SharedPreferences.Editor prefEditor;

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

    textView1 = (TextView)findViewById(R.id.textView1);
    editText1 = (EditText)findViewById(R.id.editText1);
    button1 = (Button)findViewById(R.id.button1); 
    radioGroup = (RadioGroup) findViewById(R.id.radioGroup);  

    settings = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);
        prefEditor = settings.edit();

    LoadPreferences();  
}   

@Override
 public void onClick(View v) {

SavePreferences();
LoadPreferences();
}

 private void SavePreferences() {
// TODO Auto-generated method stub

String Uname_input = editText1.getText().toString();
int checkedButton = radioGroup.getCheckedRadioButtonId();
prefEditor.clear();
prefEditor.putString("UserName", Uname_input);
prefEditor.putInt("posistion", checkedButton);
prefEditor.apply();
prefEditor.commit();
}

 private void LoadPreferences(){

String UserName = settings.getString("UserName", "");
editText1.setText( UserName + "" );
Toast.makeText(getApplicationContext(), UserName,   Toast.LENGTH_SHORT).show();
}
}

【问题讨论】:

  • 您是否真正看到过 InitialPreferences 活动?看起来你不会。
  • 我愿意...为什么不呢?
  • 我最初误读了您的条件,但我的回答仍然适用。

标签: java android sharedpreferences saving-data


【解决方案1】:

您的第一个问题是(UserName != null) 始终为真。 您将获得UserNameString UserName = settings.getString("UserName", "");,这将提供一个空字符串的默认值。如果您希望设置在未找到首选项时返回 null,则应调用 settings.getString("UserName", null)

您的第二个问题是您永远不会在 InitialPreferences 活动中的任何视图上调用 setOnClickListener(this)。因此,onClick() 永远不会被调用,也不会发生任何事情。我假设你想在onCreate() 中调用button1.setOnClickListener(this)

【讨论】:

  • 您好,感谢您的回答,我喜欢这个对新手总是有帮助的网站。更改为 settings.getString("UserName", null) 后,我现在有一个 nullpointerexception?知道为什么吗?感谢您发现 clicklistner。
  • 嗨,现在工作。我清理了项目,因为看不到它为什么抛出那个空指针并解决了它。我还必须将 if (UserName.equals("UserName")){ 更改为 if (UserName.equals(UserName)){
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多