0

Possible Duplicate:
Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application"

I've tried different things, but I still keep the same error:

android.view.WindowManager$BadTokenException: Unable to add window 

At this line:

alertDialog.show();

Can you look at the code?

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.splashscreen);

    Context mContext = this;
    alertDialog = new AlertDialog.Builder(mContext).create();

    LoadData();
}

public void LoadData()
{
    Thread t1 = new Thread(this);
t1.start();
}

private Handler handler = new Handler()
{
@Override
    public void handleMessage(Message msg) 
    {            
        if(!rssItems.isEmpty())
        {
            switch (msg.what) {
            case STOPSPLASH:
                //remove SplashScreen from view
                //splash.setVisibility(View.GONE);
                Intent intent = new Intent(
                "news.displayNews");
                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                NewsDisplayer.rssItems.clear();
                NewsDisplayer.rssItems.addAll(rssItems);

                startActivity(intent);
                Close();
                break;
            }
        }
        else
        {
            alertDialog.setCancelable(false); // This blocks the 'BACK' button
            alertDialog.setMessage("No connection.");
            alertDialog.setTitle("Error...");
            alertDialog.setButton("Again", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();     
                    LoadData();
                }
            });
            alertDialog.setButton2("Close", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();     
                    System.exit(0);
                }
            });
            alertDialog.show();
        }
    }
 };
Community
  • 1
  • 1
Japser
  • 1
  • Does this question offer any direction http://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window? – Sampson Aug 17 '11 at 13:46

2 Answers2

0

This is because the context you are using to create the alertDialog doesn't support it. So instead of mContext, try getParent() or getApplicationContext(). That might work.

Andro Selva
  • 51,960
  • 51
  • 189
  • 237
  • How come an activity doesn't support an alertDialog? I always build my dialogs that way with no issue. See that thread: http://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window – Romain Piel Aug 17 '11 at 12:34
  • I am not saying that your activity doesn't support dialog. In some cases where we are using activitygroup or adapter classes this problem arises. At these times you might have to check your context properly. I have been through this a lot many times. This was just my suggestion. And as you have suggested he is not using alertDialog.show(); in a thread. He is using it in handler, which is correct regardless to which thread it is. – Andro Selva Aug 17 '11 at 12:51
  • Hey don't worry I'm just discussing. Not saying that anyone's wrong. For my answer, I didn't pay enough attention to his code. My bad :) – Romain Piel Aug 17 '11 at 12:54
  • Tnx for your answer. I've already tried that. I found something else on the internet. if(!isFinishing){ showdialog } Maybe that is a solution? It's strang because everyting works fine on a HTC hero and a Galaxy... But in the dev console I get these erros form a "other platform/device". So it's a litte bit hard to debug... – Japser Aug 17 '11 at 14:00
-2

I think that's because you're running this in a thread. alertDialog.show(); has to be executed on the UI thread. Try using an AsyncTask instead.

EDIT: my bad, I didn't read carefully the code.

Romain Piel
  • 10,535
  • 15
  • 67
  • 105