0
我想從一個asynctask內訪問保存的偏好,但我總是不斷收到錯誤「偏好無法解決」。有任何想法嗎?以下是部分代碼:訪問asynctask偏好
public class Login extends SherlockActivity {
SharedPreferences preferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
new LongOperationLogin(this).execute();
}
}
class LongOperationLogin extends AsyncTask<String, Void, String> {
private Login longOperationContext = null;
public LongOperationLogin(Login context) {
longOperationContext = context;
}
@Override
protected String doInBackground(String... params) {
//THIS IS WHERE I NEED THE VALUE
String username = this.preferences.getString("username", "n/a");
try {
//JSON fetching
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.v("Error", "URL exc");
} catch (IOException e) {
e.printStackTrace();
Log.v("ERROR", "IOEXECPTOIn");
} catch (JSONException e) {
e.printStackTrace();
Log.v("Error", "JsonException");
}
return null;
}
@Override
protected void onPostExecute(String result) {
}
}
protected void onPreExecute() {
}
protected void onProgressUpdate(Void... values) {
}
}
在此先感謝! 羅伯特
嘗試使用'Login.this.preferences'來處理它 –
@ A - C:擊敗我。 – Squonk
@Squonk至少你有禮貌,我已經看到人們聽取我的意見,並把他們變成答案。即使我不確定我是對的。 –