Android泄露了窗口

时间:2012-07-31 00:22:57

标签: android android-asynctask

我正在使用AsyncTask来收集数据,然后开始一个新的活动,但是我得到了一个泄露的窗口。

    class GetDataTask extends AsyncTask<Void, Void, Void> {     

    @Override
    protected void onPreExecute()

    {
        mProgressDialog = new ProgressDialog(getActivity());
        mProgressDialog.setMessage("Getting schedule for "+selectedSport+"...");
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        XmlPullFeedParser xpfp = new XmlPullFeedParser(scheduleURLtoGet);
        ArrayList<Event> allEvents = xpfp.getEvents();
        scheduleToPassAlong = allEvents.toArray(new Event[allEvents.size()]);           
        return null;
    }

    @Override
    protected void onPostExecute(Void res)
    {     
        mProgressDialog.dismiss();
        startScheduleActivity();
    }
}

private void startScheduleActivity(){
    Intent intent = new Intent(this, ScheduleBoard.class);
    intent.putExtra(SPORT_NAME_EXTRA, selectedSport);
    intent.putExtra(SCHEDULE_FOR_SPORT_EXTRA, scheduleToPassAlong);
    startActivity(intent);
}

异常似乎发生在ScheduleBoard活动中,但是doInBackground中的进程没有完成。

这是堆栈,我仍然无法弄清楚发生了什么。我在doInBackground中尝试/ catch,没有任何东西被触发:

     07-31 19:48:18.667: E/WindowManager(2866): Activity com.myproj.activities.ScheduleBoard has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40557718 that was originally added here
07-31 19:48:18.667: E/WindowManager(2866): android.view.WindowLeaked: Activity com.myproj.activities.ScheduleBoard has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40557718 that was originally added here
07-31 19:48:18.667: E/WindowManager(2866):  at android.view.ViewRoot.<init>(ViewRoot.java:263)
07-31 19:48:18.667: E/WindowManager(2866):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:171)
07-31 19:48:18.667: E/WindowManager(2866):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
07-31 19:48:18.667: E/WindowManager(2866):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-31 19:48:18.667: E/WindowManager(2866):  at android.app.Dialog.show(Dialog.java:241)
07-31 19:48:18.667: E/WindowManager(2866):  at android.app.AlertDialog$Builder.show(AlertDialog.java:810)
07-31 19:48:18.667: E/WindowManager(2866):  at com.myproj.activities.ScheduleBoard.displayNoStoriesMessageAndExit(ScheduleBoard.java:114)
07-31 19:48:18.667: E/WindowManager(2866):  at com.myproj.activities.ScheduleBoard.populateTable(ScheduleBoard.java:62)
07-31 19:48:18.667: E/WindowManager(2866):  at com.myproj.activities.ScheduleBoard.onCreate(ScheduleBoard.java:56)
07-31 19:48:18.667: E/WindowManager(2866):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-31 19:48:18.667: E/WindowManager(2866):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-31 19:48:18.667: E/WindowManager(2866):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-31 19:48:18.667: E/WindowManager(2866):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-31 19:48:18.667: E/WindowManager(2866):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-31 19:48:18.667: E/WindowManager(2866):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 19:48:18.667: E/WindowManager(2866):  at android.os.Looper.loop(Looper.java:130)
07-31 19:48:18.667: E/WindowManager(2866):  at android.app.ActivityThread.main(ActivityThread.java:3687)
07-31 19:48:18.667: E/WindowManager(2866):  at java.lang.reflect.Method.invokeNative(Native Method)
07-31 19:48:18.667: E/WindowManager(2866):  at java.lang.reflect.Method.invoke(Method.java:507)
07-31 19:48:18.667: E/WindowManager(2866):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-31 19:48:18.667: E/WindowManager(2866):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-31 19:48:18.667: E/WindowManager(2866):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

见下面的代码

private class ProgressTask extends AsyncTask<String, Void, Boolean> {

private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);

protected void onPreExecute() {
    this.dialog.setMessage("Please wait");
    this.dialog.show();
}

protected Boolean doInBackground(final String... args) {
    try {
        Utilities.arrayRSS = objRSSFeed
                .FetchRSSFeeds(Constants.Feed_URL);
        return true;
    } catch (Exception e) {
        Log.e("tag", "error", e);
        return false;
    }
}

@Override
protected void onPostExecute(final Boolean success) {

    if (dialog.isShowing()) {
        dialog.dismiss();
    }
        // display UI
        UpdateDisplay();
}
}

答案 1 :(得分:0)

这种情况正在发生,因为您的活动被破坏并且ProgressDialog泄漏。您可以在进度显示或将屏幕旋转90度时按电源按钮复制此项。

您需要对进度对话框执行的操作是解除它们onPause并恢复onResume在我的练习中,跟踪此类内容会非常复杂。当我从AsyncTask开始Activity时,我将AsyncTask挂钩到Application并在完成后取消挂钩。应用程序对象存储有关哪些Activity有哪些任务正在运行的信息,并且当您单击Home并返回应用程序时可以恢复进度对话框。