Android leaked window

亡梦爱人 提交于 2019-12-25 05:31:37

问题


I am using AsyncTask to gather data, and then start a new activity, but I am getting a leaked window.

    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);
}

The exception seems to be happening in the ScheduleBoard activity, but the process inside the doInBackground does not complete.

Here is the stack, I still can't figure out what is happening. I have try/catch in the doInBackground, and nothing is getting triggered:

     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)

回答1:


See below code

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();
}
}



回答2:


This is happening because your activity get's destroyed and ProgressDialog leaks. You can duplicate this by pressing power button when progress shows or turning screen 90 degree.

What you need to do with progress dialogs is dismissing them onPause and restoring onResume In my practice it can get pretty complex to track this kind of stuff. When I start AsyncTask from Activity I hook AsyncTask to Application and unhook on completion. Application object stores info about what Activity have what task running and can resume progress dialog when you click Home and return to application.



来源:https://stackoverflow.com/questions/11731504/android-leaked-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!