Question

I need to start AsyncTask in UI thread, but the Constructor has (MainActivity parentActivity) parametr. I don't really understand why it should be implemented and how I must pass it.

Here Eclipse says "Cant resolve MainActivity to a variable." Same for Activity.MainActivity.

new DownloaderTask(MainActivity).execute();`

And the constructor.

public DownloaderTask(MainActivity parentActivity) {
    super();

    mParentActivity = parentActivity;
    mApplicationContext = parentActivity.getApplicationContext();

}
Was it helpful?

Solution

Change this line...

new DownloaderTask(MainActivity).execute();

to this...

new DownloaderTask(MainActivity.this).execute();

And you are passing Context of MainActivity not the activity...so in DownloaderTask() constructor, the parameter will be Context type not MainActivity...The constructor should look like as below...

public DownloaderTask(Context context) {
    super();

    mApplicationContext = context;

}

OTHER TIPS

you can call like following if you are calling directly from the MainActivity

new DownloaderTask(this).execute();

or if you are callling from an inner class you can call like

new DownloaderTask(MainActivity.this).execute();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top