窗口管理器坏令牌例外的Andr​​oid [英] window manager bad token exception android

查看:245
本文介绍了窗口管理器坏令牌例外的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序使用tabhost与3个标签,并当过我pressed后退按钮,我得到的 WindowManager.BadTokenException 我尝试过很多事情,但我不能够梳理出来。

下面是我的活动后台任务:

 类MyTask扩展的AsyncTask<弦乐,太虚,字符串> {    ProgressDialog pDialog;    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        pDialog =新ProgressDialog(活动);
    // pDialog =新ProgressDialog(Groupinion_MyStuff_Answer.this);        pDialog.setMessage(正在加载...);        pDialog.setCancelable(假);        尝试{
            pDialog.show();
        }赶上(例外五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
    }    @覆盖
    保护字符串doInBackground(字符串... PARAMS){
        返回Utils.getJSONString(PARAMS [0]);
    }    @覆盖
    保护无效onPostExecute(字符串结果){
        super.onPostExecute(结果);            如果(零= pDialog&放大器;!&放大器; pDialog.isShowing()){
                pDialog.dismiss();
            }
        }
    }
}

下面是我的logcat

  12月2日至26日:56:58.765:W / System.err的(30960):$ android.view.WindowManager BadTokenException:无法添加窗口 - 令牌android.app.LocalActivityManager $ @ LocalActivityRecord是42161de0无效;是您的活动运行?
十二月2日至26日:56:58.765:W / System.err的(30960):在android.view.ViewRootImpl.setView(ViewRootImpl.java:720)
十二月2日至26日:56:58.765:W / System.err的(30960):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:356)
十二月2日至26日:56:58.765:W / System.err的(30960):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234)
十二月2日至26日:56:58.765:W / System.err的(30960):在android.view.WindowManagerImpl $ CompatModeWrapper.addView(WindowManagerImpl.java:153)
十二月2日至26日:56:58.765:W / System.err的(30960):在android.view.Window $ LocalWindowManager.addView(Window.java:557)
十二月2日至26日:56:58.765:W / System.err的(30960):在android.app.Dialog.show(Dialog.java:277)
十二月2日至26日:56:58.775:W / System.err的(30960):在com.android.groupinion.Groupinion_MyStuff_Answer$MyTask.on$p$pExecute(Groupinion_MyStuff_Answer.java:269)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.os.AsyncTask.execute(AsyncTask.java:534)
十二月2日至26日:56:58.775:W / System.err的(30960):在com.android.groupinion.Groupinion_MyStuff_Answer.onCreate(Groupinion_MyStuff_Answer.java:182)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.app.Activity.performCreate(Activity.java:5203)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.app.ActivityThread.startActivityNow(ActivityThread.java:1955)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
十二月2日至26日:56:58.775:W / System.err的(30960):在com.android.groupinion.Groupinion_Mystuff.replaceContentView(Groupinion_Mystuff.java:461)
十二月2日至26日:56:58.775:W / System.err的(30960):在com.android.groupinion.Groupinion_Mystuff $ 3.onClick(Groupinion_Mystuff.java:202)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.view.View.performClick(View.java:4191)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.view.View $ PerformClick.run(View.java:17229)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.os.Handler.handleCallback(Handler.java:615)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.os.Handler.dispatchMessage(Handler.java:92)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.os.Looper.loop(Looper.java:137)
十二月2日至26日:56:58.775:W / System.err的(30960):在android.app.ActivityThread.main(ActivityThread.java:4960)
十二月2日至26日:56:58.775:W / System.err的(30960):在java.lang.reflect.Method.invokeNative(本机方法)
十二月2日至26日:56:58.775:W / System.err的(30960):在java.lang.reflect.Method.invoke(Method.java:511)
十二月2日至26日:56:58.775:W / System.err的(30960):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1038)
十二月2日至26日:56:58.775:W / System.err的(30960):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
十二月2日至26日:56:58.775:W / System.err的(30960):在dalvik.system.NativeStart.main(本机方法)


解决方案

我想这个问题是progressDialog。我建议是定义pDialog如在活动顶部(而非的AsyncTask的对象)

的目的

 类MyActivity {
   ProgressDialog pDialog;
}

和显示对话框之前,请检查活动已进入整理状态。

 如果(!activity.isFinishing)
   pDialog.show();

和在活动中您的onDestroy方法

 保护无效的onDestroy(){
    super.onDestroy();
    如果(pDialog = NULL&放大器;!&安培; pDialog.isShowing())
       pDialog.cancel();
}

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)

解决方案

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

这篇关于窗口管理器坏令牌例外的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆