user1938357
user1938357

Reputation: 1466

error in getting context in Alert Dialog in application class

I am creating an application class to perform some version checks during application launch. Below is my class.

public class MyApp extends Application {
public MyApp() {
}

@Override
public void onCreate() {
    super.onCreate();    
new checkVersionTask().execute(getApplicationContext) 
}


private class checkVersionTask extends AsyncTask<Context, Integer, Long> {        
    @Override
    protected Long doInBackground(Context... contexts) {
        TODO—version check code
    }
    protected void onPostExecute(Long result) {

            AlertDialog alertDialog;
                alertDialog = new AlertDialog.Builder(MyApp.this).create();
                alertDialog.setMessage(("A new version of app is available. Would you like to upgrade now?"));
                alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getResources().getString(R.string.Button_Text_Yes), new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which) {
                         Uri uri = Uri.parse("update URL");
                         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                         startActivity(intent);
                    }
                });
                alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,getResources().getString(R.string.Button_Text_No), new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                alertDialog.show(); 

            }
        }
        catch(Exception e){
            Toast.makeText(getApplicationContext(), "ERROR:"+e.toString(),    Toast.LENGTH_LONG).show();
        }
    }
}

}

here alertDialog.show is throwing error

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

As I understand this is because the context is not available. In the line

alertDialog = new AlertDialog.Builder(MyApp.this).create();

I tried getApplicationContext() instead of MyApp.this, still the same issue.

Can anyone suggest what's going wrong here. All the Toast statement are working fine.

Upvotes: 1

Views: 4171

Answers (2)

Kirill Kulakov
Kirill Kulakov

Reputation: 10245

You can not create a dialog within an application class since, the Dialog should be attached to a window, an Application is not UI class and has no window, so it can't show the dialog.

you can solve it by creating an activity which will show the dialog (you can pass the data as an extra with the intent), and when the data is ready fire and intent and show the dialog

Upvotes: 4

Jace J McPherson
Jace J McPherson

Reputation: 450

There are two options for giving your AsyncTask the proper context:

1) Use getBaseContext() I'm not positive if this will work, it seems to function in some situations rather than others.

2) If THAT doesn't work, you'll need to set up a constructor method for your checkVersionTask, as follows.

Context context;  //member variable of the checkVersionTask class

public checkVersionTask(Context c) {
    this.context = c;
}

Then, when you call the task in your onCreate method, or anywhere in your activity class for that matter, call it like so

new checkVersionTask(MyApp.this).execute();

Whenever you need to access context within the checkVersionTask, just say, for example

alertDialog = new AlertDialog.Builder(context).create();

Upvotes: 0

Related Questions