Androidcuckoo
Androidcuckoo

Reputation: 121

window manager bad token exception android

In my application I used tabhost with 3 tabs, and when ever I pressed the back button, I am getting WindowManager.BadTokenException I tried alot of things, but am not able to sort it out.

Here is my activity background task:

class MyTask extends AsyncTask<String, Void, String> {

    ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();


        pDialog = new ProgressDialog(activity);
    //  pDialog = new ProgressDialog(Groupinion_MyStuff_Answer.this);

        pDialog.setMessage("Loading...");

        pDialog.setCancelable(false);

        try {
            pDialog.show();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           
    }

    @Override
    protected String doInBackground(String... params) {
        return Utils.getJSONString(params[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);



            if (null != pDialog && pDialog.isShowing()) {
                pDialog.dismiss();
            }           
        }
    }       
}

Here is my Logcat

 02-26 12:56:58.765: W/System.err(30960): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@42161de0 is not valid; is your activity running?
02-26 12:56:58.765: W/System.err(30960):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:720)
02-26 12:56:58.765: W/System.err(30960):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:356)
02-26 12:56:58.765: W/System.err(30960):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234)
02-26 12:56:58.765: W/System.err(30960):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)
02-26 12:56:58.765: W/System.err(30960):    at android.view.Window$LocalWindowManager.addView(Window.java:557)
02-26 12:56:58.765: W/System.err(30960):    at android.app.Dialog.show(Dialog.java:277)
02-26 12:56:58.775: W/System.err(30960):    at com.android.groupinion.Groupinion_MyStuff_Answer$MyTask.onPreExecute(Groupinion_MyStuff_Answer.java:269)
02-26 12:56:58.775: W/System.err(30960):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
02-26 12:56:58.775: W/System.err(30960):    at android.os.AsyncTask.execute(AsyncTask.java:534)
02-26 12:56:58.775: W/System.err(30960):    at com.android.groupinion.Groupinion_MyStuff_Answer.onCreate(Groupinion_MyStuff_Answer.java:182)
02-26 12:56:58.775: W/System.err(30960):    at android.app.Activity.performCreate(Activity.java:5203)
02-26 12:56:58.775: W/System.err(30960):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
02-26 12:56:58.775: W/System.err(30960):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
02-26 12:56:58.775: W/System.err(30960):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1955)
02-26 12:56:58.775: W/System.err(30960):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
02-26 12:56:58.775: W/System.err(30960):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
02-26 12:56:58.775: W/System.err(30960):    at com.android.groupinion.Groupinion_Mystuff.replaceContentView(Groupinion_Mystuff.java:461)
02-26 12:56:58.775: W/System.err(30960):    at com.android.groupinion.Groupinion_Mystuff$3.onClick(Groupinion_Mystuff.java:202)
02-26 12:56:58.775: W/System.err(30960):    at android.view.View.performClick(View.java:4191)
02-26 12:56:58.775: W/System.err(30960):    at android.view.View$PerformClick.run(View.java:17229)
02-26 12:56:58.775: W/System.err(30960):    at android.os.Handler.handleCallback(Handler.java:615)
02-26 12:56:58.775: W/System.err(30960):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-26 12:56:58.775: W/System.err(30960):    at android.os.Looper.loop(Looper.java:137)
02-26 12:56:58.775: W/System.err(30960):    at android.app.ActivityThread.main(ActivityThread.java:4960)
02-26 12:56:58.775: W/System.err(30960):    at java.lang.reflect.Method.invokeNative(Native Method)
02-26 12:56:58.775: W/System.err(30960):    at java.lang.reflect.Method.invoke(Method.java:511)
02-26 12:56:58.775: W/System.err(30960):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-26 12:56:58.775: W/System.err(30960):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-26 12:56:58.775: W/System.err(30960):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 4736

Answers (3)

Raghavendra
Raghavendra

Reputation: 2303

The possible reason is the context of the alert dialog. You may be finished that activity so its trying to open in that context but which is already closed. Try changing the context of that dialog to you first activity beacause it won't be finished till the end.

e.g

rather than this.

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

try to use

        AlertDialog alertDialog = new AlertDialog.Builder(FirstActivity.getInstance()).create();

Upvotes: 2

Ashana.Jackol
Ashana.Jackol

Reputation: 3144

Instead of pDialog = new ProgressDialog(activity);, try:

pDialog = new ProgressDialog(yourClass.this);

Notice the argument passed.

Upvotes: 1

Can Gokdere
Can Gokdere

Reputation: 286

I guess that problem is with progressDialog. What I suggest is to define pDialog as a object in the activity at top(instead of object of the AsyncTask)

class MyActivity{
   ProgressDialog pDialog;
}

And before showing the dialog, check if activity has entered the finishing state.

if(!activity.isFinishing)
   pDialog.show();

and in your onDestroy method of the activity

protected void onDestroy(){
    super.onDestroy();
    if(pDialog != null && pDialog.isShowing())
       pDialog.cancel();
}

Upvotes: 3

Related Questions