使用齐射时,活动已泄露窗口

时间:2016-09-05 14:35:17

标签: android android-asynctask android-volley

我在volley库中使用JSONRequest,在后台使用Asynctask()......

public class DataTask extends AsyncTask<String, Void, Void> {
    public static ProgressDialog pd;
    ...

    public DataTask(Context ctx,...) {
    ...
        pd = new ProgressDialog(context);
       ...
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd.setTitle("Please wait...");
        pd.show();
    }

    @Override
    protected Void doInBackground(String... params) {
        ...
        Log.d(TAG, " url=" + url);
        JSONRequest jsonObjReq = new JSONRequest(Request.Method.GET, url, null,
                 new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                ...
                if (response.equals("success")) {
                ...
                }else if(response.equals("fail")){
                    // logout the user and redirect to login screen
                }
                Log.d(TAG, " -> pd.isShowing() = " + pd.isShowing());
                if (pd.isShowing()) { pd.hide(); }
                ...
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                pd.hide();
            }
        });
        ...
        // Adding request to request queue
        App.Instance().addToQueue(jsonObjReq);
        return null;
    }

    @Override
    protected void onPostExecute(Void data) {...}

}

我得到的错误是......

....
E/WindowManager: android.view.WindowLeaked: Activity com.volley.exmpl.DataViewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{3d416b1c V.E..... R......D 0,0-729,324} that was originally added here
                     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
                     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
                     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
                     at android.app.Dialog.show(Dialog.java:298)
                     at com.volley.exmpl.Task.LoginTask.onPreExecute(LoginTask.java:43)
                     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:591)
                     at android.os.AsyncTask.execute(AsyncTask.java:539)
                     at com.volley.exmpl.DataViewActivity.callLogin(DataViewActivity.java:281)
                     at com.volley.exmpl.DataViewActivity.ondataViewCompleted(DataViewActivity.java:283)
                     at com.volley.exmpl.Task.DataTask$1.onResponse(DataTask.java:210)
                     at com.volley.exmpl.Task.DataTask$1.onResponse(DataTask.java:112)
                     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:68)
                     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:113)
                     at android.os.Handler.handleCallback(Handler.java:739)
                     at android.os.Handler.dispatchMessage(Handler.java:95)
                     at android.os.Looper.loop(Looper.java:135)
                     at android.app.ActivityThread.main(ActivityThread.java:5254)
                     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:903)
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
D/TAG_Act: onStop()-> className::close ListActivity
D/AndroidRuntime: Shutting down VM
....
                  --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.volley.exmpl, PID: 29825
                  java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{3d416b1c V.E..... R......D 0,0-729,324} not attached to window manager
                      at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:396)
                      at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:322)
                      at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)
                      at android.app.Dialog.dismissDialog(Dialog.java:341)
                      at android.app.Dialog.dismiss(Dialog.java:324)
                      at com.volley.exmpl.Task.LoginTask$1.onResponse(LoginTask.java:120)
                      at com.volley.exmpl.Task.LoginTask$1.onResponse(LoginTask.java:71)
                      at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:68)
                      at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:113)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      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:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

任何想法,......

我已阅读https://stackoverflow.com/a/2850597/3099185的答案,我发现这些答案有些相关,但无法找到适合我情况的解决方案

还有another link is available,但我不能像那些调用那样概括,因为我的每个类都以不同的方式处理数据!?!?? !!?

先谢谢!

2 个答案:

答案 0 :(得分:1)

if(pd.isShowing()) { pd.hide(); }中写下onPostExecute(),并从progressDialog中删除所有doInBackground()相关语句。

此错误是因为AsyncTask onPreExecute()onPostExecute()main thread上运行而doInBackground()在不同thread上运行。如果您要处理来自UI的{​​{1}}以外的thread相关操作,那么您的窗口将会泄露。

答案 1 :(得分:1)

@Bhuro我在寻找类似问题的解决方案时遇到了这个问题。我设法得到了解决方案,错误实际上是使用pDialog.hide()而不是pDialog.dismiss()。如果其他人遇到这样的问题,请按如下所示初始化进度对话框;

ProgressDialog pDialog=new ProgressDialog(YourActivity.this);

然后在退出jsonRequest方法之前,在凌空响应方法或错误方法中将其关闭,就像它在上面的OP问题代码中一样。我的pDialog方法是;

private void showDialog(){
    if(!pDialog.isShowing())
        pDialog.show();
}

private void hideDialog(){
    if(pDialog.isShowing())
        pDialog.dismiss();
}

另外,我在Activity onDestroy方法中调用了dismiss方法;

  @Override
public void onDestroy(){
    super.onDestroy();
    if(pDialog!=null){
        hideDialog();
    }
}