簡體   English   中英

共享首選項不保存或加載數據

[英]Sharedpreferences not saving or loading data

我在此代碼中嘗試在“ InitialPreferences”活動中使用sharedpreferences保存一些持久數據,然后在應用程序下次啟動時使用“ StartScreen”活動搜索已保存的偏好設置,然后將用戶定向到“ InitialPreferences”或“主要活動”。 我的問題是數據沒有保存,我看不到問題,有人可以引導我解決問題並向我展示我做錯了什么嗎?

謝謝

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

您的第一個問題是(UserName != null)始終為true。 您正在使用String UserName = settings.getString("UserName", "");獲得UserName String UserName = settings.getString("UserName", ""); ,它將提供一個空字符串的默認值。 如果希望在未找到首選項的情況下設置返回null,則應調用settings.getString("UserName", null)

第二個問題是您永遠不會在InitialPreferences活動中的任何視圖上調用setOnClickListener(this) 因此,永遠不會調用onClick() ,並且什么也不會發生。 我假設您要在onCreate()調用button1.setOnClickListener(this) onCreate()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM