5

I'm trying to raise a toast from asynctask, but I'm having trouble getting my parameters right. I'm toasting from onProgressUpdate, so I'm on the UI thread, which I think is correct. I think I'm going wrong with the context parameter, what should I pass in as a value?

EDIT: Showing code below

    @Override
protected void onProgressUpdate(String... strings){
    Toast toast = Toast.makeText(MainActivity.this, strings[0], Toast.LENGTH_LONG);
    toast.show();
}

MainActivity.this is saying "No enclosing instance of the type MainActivity is accessible in scope." I'm not sure what to pass as a context instead.

Thanks

Colin
  • 2,814
  • 5
  • 27
  • 37
  • Just any context should do, what's the error message you got? – xandy Mar 22 '11 at 01:03
  • Are you calling show() on the Toast after you create it? – EboMike Mar 22 '11 at 01:07
  • No error, just red squigglys. Is there a preferred context? I tried passing MainActivity.this but got a "No enclosing instance of the type MainActivity is accessible in scope" ... new to android so this is probably a dumb question – Colin Mar 22 '11 at 01:10
  • So it's a compile error? Show us the code then. – EboMike Mar 22 '11 at 01:20
  • It's not that the toast isn't showing, just that I don't know what to pass it as a Context so I can't even compile it. – Colin Mar 22 '11 at 01:22
  • What's the context? Is this an inner class of your activity? – EboMike Mar 22 '11 at 01:32
  • This is in an AsyncTask, not sure what the context is. MainActivity executes the AsyncTask periodically – Colin Mar 22 '11 at 01:32
  • 4
    So it's a separate class? In that case, you need to pass the Context into your AsyncTask. `MainActivity.this` only works if your AsyncTask is an inner class of MainActivity. – EboMike Mar 22 '11 at 01:35
  • Or you could just use a handler.. or run a toast on the UI thread (aka runOnUiThread(...) – JoxTraex Jan 27 '12 at 04:03

3 Answers3

4

Get the Context object by calling getApplicationContext() from MainActivity and pass it as a parameter to your AsyncTask. As EboMike has pointed out, MainActivity.this would only work if your AsyncTask was an inner class.

dbyrne
  • 59,111
  • 13
  • 86
  • 103
2

If it's not an inner class declared at the point of use then MainActivity.this is likely to be out of scope. The only way to remedy the problem is to subclass AsyncTask and change the constructor to accept a context variable so you can set it in your custom class and use it from methods. Using getApplicationContext might work as well but I'm not sure how it will behave.

David K.
  • 6,153
  • 10
  • 47
  • 78
  • It doesn't need to be an anonymous inner class. A named inner class would work with `MainActivity.this` as well. Also you can't call `getApplicationContext()` from an AsyncTask. Only subclasses of `Context`, like `Activity` can call this method. – dbyrne Mar 22 '11 at 02:05
0

You can NOT do this in onProgressUpdate(). At least not this way. If Eclipse gives you this error, it is because MainActivity.this is unresolvable for it. Why? Because you are NOT in the UI Thread, so what you do is not thread-safe, because you must not access UI from another Thread.

First of all, and as told before, you should write a constructor taking a context and saving it to a global variable, so it is accessible everywhere inside the class. Then, to access UI in a thread-safe way, use one of the following:

Activity.runOnUiThread(Runnable)
View.post(Runnable)
View.postDelayed(Runnable, long)

Those are thread-safe.

Regards

Emre Yazici
  • 10,136
  • 6
  • 48
  • 55
Nabil Gardon
  • 175
  • 1
  • 1