1

I'm trying to use this code from "Android Recipes":

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("FetchAndPopTask.doInBackground exception");
builder.setMessage(e.getMessage());
builder.setPositiveButton("OK", null);
builder.create().show();

...but don't know what I should replace "context" with. I've tried the .java file's class, the immediate class, and "this" but none of them compile.

In more context, the code is:

public class SQLiteActivity extends ActionBarActivity {

private FetchAndPopTask _fetchAndPopTask;

. . .

private class FetchAndPopTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        . . .
        try {
            . . .
        } catch (Exception e) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this); // <= "context"...?
            builder.setTitle("If I go blind, I'll use a Service Platypus (instead of a Service Dog)");
            builder.setMessage(e.getMessage());
            builder.setPositiveButton("OK", null);
            builder.create().show();
            return result;
    }

I tried all of the following:

AlertDialog.Builder builder = new AlertDialog.Builder(SQLiteActivity);
AlertDialog.Builder builder = new AlertDialog.Builder(FetchAndPopTask);
AlertDialog.Builder builder = new AlertDialog.Builder(this);

...but none compile; so what does "context" need to be here?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

5 Answers5

3

AlertDialog.Builder(SQLiteActivity.this) should probably work

But take a look at this question

EDIT!!!!! Sorry, didn't notice you're trying to show it in non-UI thread. Please place it in constructor or in onPreExecute()/onPostExecute() methods

Community
  • 1
  • 1
Sam
  • 1,652
  • 17
  • 25
3

doInBackground() will be run on a background thread. You cannot touch your UI on a non-UI thread. This includes displaying dialogs. Remove the AlertDialog from doInBackground(). You can put it in e.g. onPostExecute() that runs on UI thread. In there you can use YourActivityName.this to refer to the outer class this to be used as a Context.

laalto
  • 150,114
  • 66
  • 286
  • 303
1

You have to pass an Activity to this constructor.

You have to add this in the FetchAndPopTask class:

private SQLiteActivity context;

public FetchAndPopTask(SQLiteActivity context) {
    this.context = context;
}

Then, in the SQLiteActivity class, you have to pass this context by using this keyword (as you are in an activity, it refers to it):

/*...*/
FetchAndPopTask task = new FetchAndPopTask(this);
task.execute();
/*...*/
G.T.
  • 1,557
  • 1
  • 12
  • 24
  • You can see that async task class is non-static so you can use 'SQLiteActivity.this' to be shorter – Sam Jun 16 '14 at 21:34
  • @SamN-a I did not know that. Indeed it is shorter and more convenient. Thanks ;) – G.T. Jun 16 '14 at 21:36
  • Would not say it's more convenient because it may cause some extra work when refactoring(when you put the asynctask out of this class, for example). However it's shorter ;) – Sam Jun 16 '14 at 21:40
1
new AlertDialog.Builder(this);

the this means that you are getting the pointer/reference of FetchAndPopTask so instead of using this use the pointer/reference of your SQLiteActivity by calling SQLiteActivity.this

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
1

Define a constructor for your class and pass the context there

private class FetchAndPopTask extends AsyncTask<String, String, String> {

private Context mContext;

public FetchAndPopTask(Context context){
    mContext = context;
}

@Override
protected String doInBackground(String... params) {
    . . .
    try {
        . . .
    } catch (Exception e) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext); // <= use the variable here
        builder.setTitle("If I go blind, I'll use a Service Platypus (instead of a Service Dog)");
        builder.setMessage(e.getMessage());
        builder.setPositiveButton("OK", null);
        builder.create().show();
        return result;
}
Carlos J
  • 2,965
  • 4
  • 17
  • 28