2013-08-30 34 views
0

我有登錄活動和AsyncTask類。AsyncTask中的SharedPreferences獲得了emoaty值

所以當轉發一些值來登錄到AsyncTask的活動和測試與吐司味精我得到空值的味精。

我已經測試過使用上下文和許多方法,但不工作。

我sharedprefrences在loginactivity:

  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    Editor editor = prefs.edit(); 
    editor.putString("logintemp", login1); 
    editor.putString("passwordtemp", pass1); 
    editor.apply();// commit is important here. 

我的代碼在doInBackground在的AsyncTask類:

Activity mActivity; 
    Context context ; 

      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mActivity.getApplicationContext()); 
      String username = preferences.getString("logintemp",""); 

    Toast.makeText(mActivity.getApplicationContext(), username , Toast.LENGTH_LONG).show(); 

回答

0

在獲取用戶名,設置時無法找到它的默認值 「」 。 和你的後臺任務中,您使用context.getSharedPresferences,但你使用mActivity.getApplicationContext()

我覺得可能是兩個或他們代表不同的上下文

+0

我已更新我的代碼 – user2734352

0

commit()不更新之間的衝突內存中的值,而只是更新持久存儲上的值。因此,如果您的API級別爲10,那麼我會建議您使用apply(),這也會更新內存中的值。

//for example 
editor.apply(); 
+0

同樣的結果給出空值 – user2734352

+0

我已更新我的代碼 – user2734352

相關問題