0

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();

}
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
Mabjik
  • 255
  • 3
  • 5
  • 10

2 Answers2

0

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;

}
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • Yeah! Thats what I thought about, but this programm is actually a task with missing blocks of code. And this constructor is already written and I must not change it. So i pass an instance and then get context already in the constructor. – Mabjik Feb 26 '14 at 04:11
  • can you post your full `AsyncTask`? so that I can help you more? and I trying to figure out the problem by passing an `MainActivity` instance...thats working fine for me. – Hamid Shatu Feb 26 '14 at 04:26
  • Looks like AsyncTask activity is working now. But i got problems with the receiver. http://stackoverflow.com/questions/22051532/receiver-not-registered – Mabjik Feb 26 '14 at 19:40
0

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();
dustz
  • 3
  • 4
stinepike
  • 54,068
  • 14
  • 92
  • 112