What should I use for the Android context here?

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?

Upvotes: 1

Views: 67

Answers (5)

Sam
Sam

Reputation: 1662

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

Upvotes: 3

Carlos J
Carlos J

Reputation: 3045

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;
}

Upvotes: 1

laalto
laalto

Reputation: 152907

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.

Upvotes: 3

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

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

Upvotes: 1

G.T.
G.T.

Reputation: 1557

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();
/*...*/

Upvotes: 1

Related Questions