0

我正在使用 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 中有 try/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)
4

2 回答 2

0

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

你需要对进度对话框做的是解除它们onPause并恢复onResume在我的实践中,跟踪这类东西可能会变得非常复杂。当我AsyncTask从我开始时,我会在完成时Activity上钩并脱钩。应用程序对象存储有关哪些 Activity 正在运行哪些任务的信息,并且当您单击主页并返回应用程序时可以恢复进度对话框。AsyncTaskApplication

于 2012-07-31T23:55:18.953 回答
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();
}
}
于 2012-07-31T06:38:06.710 回答