我正在尝试从异步任务中访问已保存的首选项,但我总是不断收到错误“无法解决首选项”。有任何想法吗?这是代码的一部分:
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) {
}
}
提前致谢!罗伯特