3

I want to create an alert dialog from a handler which gets signaled when a thread terminates this is my code which causes:

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

Handler handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {

        if (dialog != null)
        {
            dialog.dismiss();
            dialog = null;
        }

        switch (serverResponseCode)
        {
        case 200:
        {
            AlertDialog alertDialog;
            alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
            alertDialog.setTitle("Super :)");
            alertDialog.setMessage("Poza a fost trimisa cu success.");
            alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {

                  public void onClick(DialogInterface dialog, int id) {

                     finish();

                } }); 
            alertDialog.show();
            serverResponseCode = -1;

            break;
        }
        default:
        {
            AlertDialog alertDialog;
            alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
            alertDialog.setTitle("Eroare :(");
            alertDialog.setMessage("Eroare la trimiterea pozei.");
            alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {

                  public void onClick(DialogInterface dialog, int id) {

                     finish();

                } }); 
            alertDialog.show();

            break;
        }
        }


         }
     };
Ilona
  • 357
  • 3
  • 10
opc0de
  • 11,557
  • 14
  • 94
  • 187

3 Answers3

5

The problem probably is getApplicationContext() isn`t yout activity context.

    alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
    //should be change to
    alertDialog = new AlertDialog.Builder( YourActivity.this ).create();
markwu
  • 51
  • 2
3

You can't use application context to create dialogs. Use an Activity context instead.

Also, that way of creating dialogs is bound to create problems later on, especially if the activity is restarted for whatever reason. You should either use Dialog fragments, or managed dialogs (Activity's showDialog() method)

hrnt
  • 9,882
  • 2
  • 31
  • 38
1

I am sure you are doing WebService request/response inside the Android, then i would suggest you to implement AsyncTask which is known as Painless Threading in android because you don't need to bother about Thread management.

FYI, inside doInBackground() - write web call logic, inside onPostExecute() - show alert dialog which you want to display.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • Note that the answer is not really that simple. Android's Activity lifecycle causes problems for AsyncTask - please see http://stackoverflow.com/questions/3357477/is-asynctask-really-conceptually-flawed-or-am-i-just-missing-something for more details. – hrnt May 08 '12 at 11:29