为什么这个窗口发生泄漏?

时间:2013-11-18 05:46:34

标签: android android-asynctask android-activity memory-leaks

我有一个返回搜索结果的应用。从结果列表中,可以单击特定项目,然后显示详细信息页面。每当我进行搜索并返回结果时,我都会收到以下窗口泄漏错误,查看特定结果的详细信息,返回结果列表,然后单击另一个以查看详细信息。单击第二个结果并加载详细信息页面时出现错误。

这是错误的logcat:

11-17 21:32:47.876: E/WindowManager(5945): Activity com.tforan.blobtag4.PlaceActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40707298 that was originally added here
11-17 21:32:47.876: E/WindowManager(5945): android.view.WindowLeaked: Activity com.tforan.blobtag4.PlaceActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40707298 that was originally added here
11-17 21:32:47.876: E/WindowManager(5945):  at android.view.ViewRoot.<init>(ViewRoot.java:278)
11-17 21:32:47.876: E/WindowManager(5945):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
11-17 21:32:47.876: E/WindowManager(5945):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
11-17 21:32:47.876: E/WindowManager(5945):  at android.view.Window$LocalWindowManager.addView(Window.java:433)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.Dialog.show(Dialog.java:265)
11-17 21:32:47.876: E/WindowManager(5945):  at com.tforan.blobtag4.PlaceActivity$FactualRetrievalTask.onPreExecute(PlaceActivity.java:703)
11-17 21:32:47.876: E/WindowManager(5945):  at android.os.AsyncTask.execute(AsyncTask.java:391)
11-17 21:32:47.876: E/WindowManager(5945):  at com.tforan.blobtag4.PlaceActivity.factualQuery(PlaceActivity.java:295)
11-17 21:32:47.876: E/WindowManager(5945):  at com.tforan.blobtag4.PlaceActivity.onCreate(PlaceActivity.java:217)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1873)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.ActivityThread.access$1500(ActivityThread.java:135)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
11-17 21:32:47.876: E/WindowManager(5945):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 21:32:47.876: E/WindowManager(5945):  at android.os.Looper.loop(Looper.java:150)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.ActivityThread.main(ActivityThread.java:4358)
11-17 21:32:47.876: E/WindowManager(5945):  at java.lang.reflect.Method.invokeNative(Native Method)
11-17 21:32:47.876: E/WindowManager(5945):  at java.lang.reflect.Method.invoke(Method.java:507)
11-17 21:32:47.876: E/WindowManager(5945):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
11-17 21:32:47.876: E/WindowManager(5945):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
11-17 21:32:47.876: E/WindowManager(5945):  at dalvik.system.NativeStart.main(Native Method)

为什么会发生这种情况?我该怎么做才能摆脱这个错误?很高兴根据需要提供更多代码。感谢。

编辑 - 这是有问题的AsyncTask的代码:

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(PlaceActivity.this);
        dialog.setMessage("Loading...");
        dialog.setIndeterminate(true);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setCancelable(true);
        dialog.show();
    }

    @Override
    protected RowResponse doInBackground(RowQuery... query) {
        RowResponse resp = factual.fetchRow("restaurants", id, query[0]);
        return resp;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
    }

    @Override
    protected void onPostExecute(RowResponse resp) {
        StringBuffer sb = new StringBuffer();
        String today = findCurrentDay(currentDay);
        JSONArray todayHours = null;
        Log.i("factual response", resp.toString());

        for (Map<String, Object> restaurant : resp.getData()) {

            //..a bunch of JSON parsing..

        dialog.dismiss();
    }

1 个答案:

答案 0 :(得分:0)

我猜你在代码中的某处显示对话框,并创建使用活动上下文的对话框。窗口泄漏的发生只是因为您正在显示对话框时完成活动。

确保从搜索返回时没有做上述事情。