错误令牌异常 - 无法添加窗口。仅在活动重新启动后发生

时间:2012-08-08 18:20:59

标签: java android exception views android-view

所以,我有这个代码,我在那里做一个HttpPost并等待一个回复给一个处理程序的响应。在处理程序块中,我显示了ProgressDialog。在我使用此代码

重新启动活动之前,一切似乎都能正常工作
Intent intent = getIntent();
finish();
startActivity(intent);

第一个处理程序的代码是这个

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (0 < msg.what) {
        } else {
            if (msg.getData() != null) {
                String s = msg.getData().getString(getResources().getString(R.string.BundleTextKey));
                if (s!=null) {
                                try{
                                    POSTMethods post_obj = new POSTMethods(messageHandleralert,context);
                                    String sender = ValuesHolder.user;
                                    mprogDialog = ProgressDialog.show(Tab_Activity.this, "", "creating pending request");
                                    post_obj.createPendingRequest(sender);

                                }
                                catch(Exception e){
                                    e.printStackTrace();
                                    Toast.makeText(Tab_Activity.this, "Try Again", Toast.LENGTH_LONG).show();
                                }
                }
            }
        }
    }
};

我通过createPendingRequest进行Post调用,此响应由另一个处理程序处理,这是

public Handler messageHandleralert = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        switch(msg.what){
        case 5:
            try{
                HashMap<String, String> map = new HashMap<String, String>();
                DateClass date_obj = new DateClass();
                String date = date_obj.getDate();
                map.put("date",date);
                map.put("sender_email", sender);
                mOutAdapter.insert(map, 0);
                mprogDialog.dismiss();
                String sData = date+" "+sender;
                System.out.println(active);
                showAlert showDialog = new showAlert(mOutAdapter,context,null,null);
                AlertDialog.Builder alert = showDialog.createAlert("Received Payment Request", "Confirm Details of the Payee", true, false,true,sData);
                alert.show();
            }
            catch(Exception e){
                e.printStackTrace();
                mprogDialog.dismiss();
                Toast.makeText(Tab_Activity.this, "Error... Do it Again", Toast.LENGTH_LONG).show();
            }
            break;
        case 6:
            try{
                Toast.makeText(Tab_Activity.this, "Error. Check Connection and Try Again", Toast.LENGTH_LONG).show();
                mprogDialog.dismiss();
            }
            catch(Exception e){
                e.printStackTrace();
            }
            break;
        default:
            break;
        }
    }
};

因此错误出现在我的第一个处理程序块中的ProgressDialog行。

mprogDialog = ProgressDialog.show(Tab_Activity.this, "", "creating pending request");

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41520070 is not valid; is your activity running?

实际上,我可以通过不显示任何进度来摆脱这一行。但是我的第二个处理程序块中的AlertDialog是必需的,它会在重新启动活动时抛出相同的错误。

我试过包括

(!isFinishing())

但这不起作用。如果我使用它,活动重启时永远不会达到代码。

是否有解决此问题的方法?或者我做错了什么?一些帮助甚至链接将不胜感激

logcat的:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@414f3fa8 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:646)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
at android.view.Window$LocalWindowManager.addView(Window.java:537)
at android.app.Dialog.show(Dialog.java:278)
at android.app.ProgressDialog.show(ProgressDialog.java:116)
at android.app.ProgressDialog.show(ProgressDialog.java:99)
at android.app.ProgressDialog.show(ProgressDialog.java:94)
at org.example.pay.Tab_Activity$1.handleMessage(Tab_Activity.java:159)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案