0

当在后台执行任务时,我正在显示一个进程对话框,问题是,它在工作完成后没有被取消/解雇。

这是我的 AsyncTask 类:

public class FetchEmployeeAsyncTask extends AsyncTask<String, Void, ArrayList<Employee> >   {

private CaptureActivity activity;
private ProgressDialog progressDialog;
public FetchEmployeeAsyncTask(CaptureActivity nextActivity) {
    this.activity = nextActivity;
}

@Override
protected ArrayList<Employee> doInBackground(String... url) {
    // TODO Auto-generated methoVoidd stub
    ArrayList<Employee> employees = null;
    for(String employeeUrl : url){
        employees = fetch(employeeUrl);
    }
    return employees;
}

private ArrayList<Employee> fetch(String url) {
    // TODO Auto-generated method stub
    ArrayList<Employee> employees = null;
    String response = null;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);
        employees = EmployeeXMLParser.employeeParser(response);
        System.out.println("Size in fetch "+employees.size());

        //System.out.println("Employee Name :: " + employees.get(0).getFirstName() + " " + employees.get(0).getLastName());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } /*catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        System.out.println("Error parsing the response :: " + response);
        e.printStackTrace();
    }*/
    return employees;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
    progressDialog = new ProgressDialog(activity);
    progressDialog.setCancelable(true);
    progressDialog.setTitle("Fetching Employees!!");
    progressDialog.setMessage("Please wait...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setProgress(0);
    progressDialog.show();
}

@Override
public void onPostExecute(ArrayList<Employee> employees){
    super.onPostExecute(employees);

    System.out.println("in post execxute "+employees.size());
    progressDialog.dismiss();
    activity.showEmployees(employees);


}
}

这个类是从一个活动类中调用的。这是我的活动课:

public class CaptureActivity extends Activity {

private String url = "";

FetchEmployeeAsyncTask employeeAsyncTask = new FetchEmployeeAsyncTask(this);
SelectedEmployeeAsyncTask selectedEmployeeAsyncTask = new SelectedEmployeeAsyncTask(this);
private ArrayList<Employee> employees = null;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    employeeAsyncTask.execute(new String[] {url});

    System.out.println("Status "+employeeAsyncTask.getStatus());

    setContentView(R.layout.activity_capture);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_capture, menu);
    return true;
}



public void showEmployees(ArrayList<Employee> employees) {
    // TODO Auto-generated method stub
    System.out.println("Status 2  "+employeeAsyncTask.getStatus());
    TableLayout employeeTable = (TableLayout) findViewById(R.id.main_table);

    TableRow header = new TableRow(this);
    header.setId(100);
    header.setBackgroundColor(Color.rgb(255, 165, 0));
    //header.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    TextView empCodeHeader = new TextView(this);
    empCodeHeader.setId(200);
    empCodeHeader.setText("Employee Code");
    empCodeHeader.setTextSize(16);
    empCodeHeader.setPadding(2,2,2,2);
    empCodeHeader.setWidth(200);
    header.addView(empCodeHeader);

    TextView empNameHeader = new TextView(this);
    empNameHeader.setId(201);
    empNameHeader.setText("Employee Name");
    empNameHeader.setTextSize(16);
    empNameHeader.setPadding(2,2,2,2);
    empNameHeader.setWidth(300);
    header.addView(empNameHeader);}
4

1 回答 1

0

将 CaptureActivity 代码更改为:

    public class CaptureActivity extends Activity {

    private String url = "";

    FetchEmployeeAsyncTask employeeAsyncTask ;
    SelectedEmployeeAsyncTask selectedEmployeeAsyncTask;
    private ArrayList<Employee> employees = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_capture);  //<< call here setContentView

       employeeAsyncTask = new FetchEmployeeAsyncTask(this);
       selectedEmployeeAsyncTask = new SelectedEmployeeAsyncTask(this);

       employeeAsyncTask.execute(new String[] {url});

        System.out.println("Status "+employeeAsyncTask.getStatus());


    }
   // your code here...

并确保 url 不为空

于 2013-01-31T06:15:37.023 回答