2012-09-19 50 views
6

我想使用sharedpreferences來檢查用戶是否在他們開始使用應用程序之前登錄。我保存shredpreferences用戶名用戶登錄時。android-sharedpreferences返回空值

Login.java

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login);  
     Button btnLogin = (Button) findViewById(R.id.buttonlogin);  
     btnLogin.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View adapt) { 
       EditText usernameEditText = (EditText) findViewById(R.id.EditUserName); 
       userName = usernameEditText.getText().toString(); 
       EditText passwordEditText = (EditText) findViewById(R.id.EditPassword); 
       userPassword = passwordEditText.getText().toString();    
       if (userName.matches("") && userPassword.matches("")) { 
        toast("Please enter Username and Password"); 
        return; 
       } else if (userName.matches("") || userName.equals("")) { 
        toast("Please enter Username"); 
        return; 
       } else if (userPassword.matches("") || userPassword.equals("")) { 
        toast("Please enter Password"); 
        return; 
       } else { 
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); 
        SharedPreferences.Editor editor = sharedPreferences.edit(); 
        editor.putString("MEM1", userName); 
        editor.commit(); 
        new DownloadFilesTask().execute(); 
       }   
      } 
     }); 
    } 

    private void toast(String text) { 
     Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); 
    } 

    private class DownloadFilesTask extends AsyncTask<Void, Void, Void> { 
     protected void onPreExecute() { 
     } 

     protected void onPostExecute(Void result) { 
      toast("user logged in"); 
      startActivity(new Intent(Login.this, MainActivity.class)); 
      finish(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) {   
      return null; 
     } 
    } 

} 

,我試圖檢查用戶名值之前,我開始我的mainactivity。

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); 
    String username =sharedPreferences.getString("MEM1", ""); 
    if(username.equalsIgnoreCase("")||username.length()==0) 
    { 
     toast("username is null"); 
     startActivity(new Intent(MainActivity.this, Login.class)); 
     finish(); 
    } 

但用戶名始終爲空。請幫忙。謝謝

+0

時,如果您的用戶名曾是空你的應用程序會崩潰,因爲它不是可以調用一個空值的方法。如果你想檢查空字符串,最好使用isEmpty()。 –

回答

9

寫下面的代碼,而不是你的代碼保存用戶名到sharedpreferences。

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); 
SharedPreferences.Editor editor = myPrefs.edit(); 
editor.putString("MEM1", userName); 
editor.commit(); 

並使用下面的代碼獲取首選項值。

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); 
String username = myPrefs.getString("MEM1",""); 
+0

@ chinna_82在日誌中打印用戶名值並告訴我發生了什麼... –

17

調用getPreferences意味着您必須處於相同的活動狀態。使用getSharedPreferences可讓您分享活動之間的偏好。你只需要爲喜好定義對罵getSharedPreferences("Pref name here", MODE_PRIVATE);

1
private final String TAXI_SPREF = "TAXI_SHARED_PREFERENCES"; 

//set-save data to shared preferences 
SharedPreferences.Editor sPEditor = getSharedPreferences(TAXI_SPREF, MODE_PRIVATE).edit(); 
sPEditor.putInt("USERID", etUserID.getText().toString()); 
sPEditor.putString("EMAIL", etEmail.getText().toString()); 
sPEditor.apply(); 

//get data from shared preferences 

SharedPreferences sharedPreferences = getSharedPreferences(TAXI_SPREF, MODE_PRIVATE); 
int userID = sharedPreferences.getInt("USERID", 0); 
string email = sharedPreferences.getString("EMAIL", 0); 
////////  or   ///// 
String email = getSharedPreferences(TAXI_SPREF, MODE_PRIVATE).getString("EMAIL","EMPTY"); 
    if (!email.equals("EMPTY")){ 
     etEmail.setText(email); 
    }