【发布时间】:2012-10-22 05:47:25
【问题描述】:
这个 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的基础知识:mindprod.com/jgloss/scope.html
标签: android arraylist android-asynctask