這個AsyncTask代碼有什麼問題阻止它返回一個數組進度......我在指定的行上得到「進度無法解析爲變量」。AsyncTask返回變量無法解析
public class FetchProgress extends AsyncTask<OpportunityActivity, Void, ArrayList<Progress>> {
private OpportunityActivity opportunityActivity;
@Override
protected ArrayList<Progress> doInBackground(OpportunityActivity... activity) {
opportunityActivity = activity[0];
String readFeed = readFeed();
// this JSON feed is delivered correctly
try {
JSONObject json = new JSONObject(readFeed);
JSONObject jsonObject = new JSONObject(json.optString("ShowProgress"));
Progress progress = new Progress();
progress.setShowProgress(jsonObject.getBoolean("ShowProgress"));
progress.setTarget(jsonObject.getInt("Target"));
// the above values are set correctly and appear if I use Log
} catch (Exception e) {
e.printStackTrace();
}
// HERE'S THE ISSUE:
// Eclipse reports "progress cannot be resolved to a variable"
return progress;
}
這裏的OnPostExecute:
public void onPostExecute(ArrayList<Progress> progress) {
opportunityActivity.updateProgress(progress);
}
這裏的類:
public class Progress {
private boolean showprogress;
private int target;
public boolean getShowProgress() {
return showprogress;
}
public void setShowProgress(boolean showprogress) {
this.showprogress = showprogress;
}
public int getTarget() {
return target;
}
public void setTarget(int target) {
this.target = target;
}
}
這裏的onPostExecute:
public void onPostExecute(ArrayList<Progress> progress) {
opportunityActivity.updateProgress(progress);
}
而且該方法將觸發在那裏我可以不訪問傳遞的ArrayList:
public void updateProgress(ArrayList<Progress> progress) {
progress_text = (TextView)findViewById(R.id.progress_text);
progress_text.setText("Target: " + progress.getTarget());
}
您應該首先了解Java的基礎知識:http://mindprod.com/jgloss/scope.html –