2013-08-01 112 views
0

我有這段代碼,我試圖在「InitialPreferences」活動中使用sharedpreferences保存一些持久數據,然後「StartScreen」活動在應用下次啓動時搜索保存的首選項,用戶轉到「InitialPreferences」或「MainActivity」。 我的問題是數據沒有被保存,我看不到問題,任何人都可以指導我解決問題並告訴我我做錯了什麼?Sharedpreferences不保存或加載數據

感謝

啓動畫面活動:

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

你有沒有真的看到InitialPreferences活動?它看起來不像你會。 –

+0

我呢...我爲什麼不呢? –

+0

我最初誤解了你的條件,但我的答案仍然適用。 –

回答

1

你的第一個問題是:(UserName != null)始終是真實的。 你得到UserNameString UserName = settings.getString("UserName", "");,它將提供一個空字符串的默認值。如果您希望設置在找不到首選項時返回null,則應該致電settings.getString("UserName", null)

第二個問題是,您從未在InitialPreferences活動的任何視圖上致電setOnClickListener(this)。因此onClick()永遠不會被調用,並且什麼都不會發生。我假設你想在onCreate()中撥打button1.setOnClickListener(this)

+0

嗨,謝謝你的回答,我喜歡這個網站對新手總是有幫助的。在改爲settings.getString(「UserName」,null)後,我現在有一個nullpointerexception?任何想法爲什麼?感謝您發現clicklistner。 –

+0

嗨,現在工作。我清理了這個項目,因爲看不出爲什麼它會拋出那個空指針並解決了它。我也必須改變if(UserName.equals(「UserName」)){if(UserName.equals(UserName)){ –