WindowManagerBadTokenException无法添加窗口,您的活动是否正在运行?

时间:2016-06-15 11:09:38

标签: android

我有一个Android项目,所有源代码都没有错误,但有时会给我一个力量关闭。

这是我的代码:

Handler mHanlder = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case 0://车主下单
                Toast.makeText(CarOwnerPlaceOrderActivity.this, "下单成功", Toast.LENGTH_SHORT).show();
                finish();
                break;
        }
    }
};
 String url = getString(R.string.server_url) + URLMap.COMMIT_CAR_WAY_BILL;
    Http.exchange(HttpRequest.HttpMethod.POST,url,params,mHanlder,PLACEORDER,mContext);

HTTP代码

public class Http {
private static final String TAG = "Http";
public static CustomProgressDialog customProgressDialog;
public static void exchange(HttpRequest.HttpMethod method, final String url,final RequestParams params, final Handler handler, final int what  ,final Context context){
    HttpUtils httpUtils = new HttpUtils();
    httpUtils.send(method, url, params, new RequestCallBack<String>() {
        @Override
        public void onStart() {
            super.onStart();
            StringBuffer sb = new StringBuffer();
            if (params.getQueryStringParams().size() > 0) {
                for (int i = 0; i < params.getQueryStringParams().size(); i++) {
                    sb.append(params.getQueryStringParams().get(i));
                    if (i + 1 < params.getQueryStringParams().size()) {
                        sb.append("&");
                    }
                }
                LogUtil.Log("url= " + url + sb);
            }
            if (customProgressDialog == null) {
                customProgressDialog = new CustomProgressDialog(context,"加载中...");
            }
            if (url.equals(context.getString(R.string.server_url) + URLMap.ADVIERTISE) || url.equals(context.getString(R.string.server_url) + URLMap.RECOMMENDLIST)){

            } else {
                customProgressDialog.show();
            }

        }

        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            Log.d(TAG + "->" + url, "onSuccess: "+ responseInfo.result);
            customProgressDialog.dismiss();
            try {
                JSONObject data = new JSONObject(responseInfo.result);
                int state = Integer.valueOf(data.getString("state"));
                if (state == 1){
                    Message message = Message.obtain();
                    message.what = what;
                    message.obj = responseInfo.result;
                    handler.sendMessage(message);
                } else {
                    String mess = data.getString("message");
                    Toast.makeText(context, mess, Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e){
                e.printStackTrace();
            }


        }

        @Override
        public void onFailure(HttpException e, String s) {
            Log.e(url, "onFailure: " + s);
            customProgressDialog.dismiss();
            Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
            Log.e(TAG, "onFailure: " + e.getExceptionCode());
            Log.e(TAG, "onFailure: " + s );
            e.printStackTrace();
        }
    });
}

logcat的:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@148da99e is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:562)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:298)
at cn.com.hailun.HTTP.Http$1.onStart(Http.java:51)
at com.lidroid.xutils.http.HttpHandler.onProgressUpdate(HttpHandler.java:200)
at com.lidroid.xutils.task.PriorityAsyncTask$InternalHandler.handleMessage(PriorityAsyncTask.java:385)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:139)
at android.app.ActivityThread.main(ActivityThread.java:5302)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

问题

某个时候有这种情况,有时候不会发生。

并且,他需要Context是活动,我尝试过其他Context,我试过,为什么?

非常感谢!!

1 个答案:

答案 0 :(得分:0)

您正尝试在已完成的活动上显示对话框。您可以使用isDestroyed()检查它是否已被销毁,如果它已被销毁,则不会显示该对话框。