在以下代码段中:
public class ExternalClass {
private int num = 1;
public void backgroundTask() {
new HttpTask().execute();
}
public int getNum() {
return num;
}
private class HttpTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
//Do stuff...
}
@Override
protected void onPostExecute(String result) {
//Do stuff...
ExternalClass.this.num = 2;
}
}
在活动中:
...
ExternalClass ec = new ExternalClass();
ec.backgroundTask();
int myNum = ec.getNum(); //NUM IS 1, NOT 2!!
我究竟做错了什么?一切正常,我的 doInBackground() 和 onPostExecute() 成功完成,但字段“num”没有改变。我试过“num = 2”甚至“this.num = 2”(我知道不正确,但是......)。