pawel
pawel

Reputation: 6136

Why my AsyncTask doesn't finish?

I've created an asynctask in my MapActivity, here it is:

class ReadLocations extends AsyncTask<String, String, String> {

    GeoPoint apoint1;
    GeoPoint apoint2;
    ArrayList<GeoPoint> Locations = new ArrayList<GeoPoint>(); 


    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(MyMapLocationActivity.this);
        pDialog.setMessage("DONE");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();


    }

    protected String doInBackground(String... args) {

        return null; 
    }


    protected void onPostExecute() {
        // dismiss the dialog once done
        pDialog.dismiss();

    }

}

I'm trying to execute it this way:

public class MyMapLocationActivity extends MapActivity {

private MapView mapView;
private ProgressDialog pDialog;  
private ProgressDialog eDialog;  


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 

ReadLocations Read = new ReadLocations();
Read.execute();

 ...

My controlling dialog never disappears - it seems like my onPostExecute method is not called - why is that?

Upvotes: 0

Views: 688

Answers (3)

Rob
Rob

Reputation: 4753

You have not correctly overridden onPostExecute, the result parameter is missing

It should be something like

@Override
protected void onPostExecute(String result) {
    // dismiss the dialog once done
    pDialog.dismiss();
}

Upvotes: 1

ashakirov
ashakirov

Reputation: 12350

in Eclipse the best and the easiest way to override or implement methods of super classes correctly is:

  1. Focus cursor on your AsyncTask body. Then mouse right click--> source --> override/implement methods.
  2. Choose necessary methods.
  3. Click OK. Methods will automatically add into your class.

Also you can generate constructors, getters/setters etc this way .

Upvotes: 0

user370305
user370305

Reputation: 109237

Becoz, your AsyncTask's onPostExecute() have not argument. which is return by doInBackground() .

SO override correctly both methods.

Something like,

@Override
protected String doInBackground(String... args) {  // Return type of same as argument of onPostExecute() 
    return null; 
}

@Override
protected void onPostExecute(String result) { // Add String argument in onPostExecute()
    // dismiss the dialog once done
    pDialog.dismiss();
}

As possible of execution of doInBackground() is fast because there is no any other work implementation in it. only one return statement..

Upvotes: 1

Related Questions