使用 progressDialog 会出现 window 泄漏错误并且不会在 Android 中关闭

Using progressDialog gives window leak error and doesn't dismiss in Android

在从服务器获取数据之前,我在所有活动中都使用 ProgressDialog。我在谷歌上搜索了很多并尝试了所有可能的解决方案,但总是出现以下错误并且进度对话框没有得到 dismissed.I 我正在从 MainActivity 调用 AssignedJobs,它也使用进度对话框。谁能告诉我如何修复 it.My 代码和错误日志如下:

错误日志

      E/WindowManager(8870): android.view.WindowLeaked: Activity com.example.rigrunners.AssignedJobs has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{2023f9f4 V.E..... R.....ID 0,0-575,144} that was originally added here
       E/WindowManager(8870):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:367)
       E/WindowManager(8870):   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
       E/WindowManager(8870):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
       E/WindowManager(8870):   at android.app.Dialog.show(Dialog.java:306)
       E/WindowManager(8870):   at com.example.rigrunners.AssignedJobs.startLoading(AssignedJobs.java:78)
       E/WindowManager(8870):   at com.example.rigrunners.AssignedJobs.load_assigned_jobs(AssignedJobs.java:198)
        E/WindowManager(8870):  at com.example.rigrunners.AssignedJobs.onCreate(AssignedJobs.java:156)
        E/WindowManager(8870):  at android.app.Activity.performCreate(Activity.java:5933)
        E/WindowManager(8870):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        E/WindowManager(8870):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
         E/WindowManager(8870):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
        E/WindowManager(8870):  at android.app.ActivityThread.access0(ActivityThread.java:149)
        E/WindowManager(8870):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
         E/WindowManager(8870):     at android.os.Handler.dispatchMessage(Handler.java:102)
         E/WindowManager(8870):     at android.os.Looper.loop(Looper.java:211)
         E/WindowManager(8870):     at android.app.ActivityThread.main(ActivityThread.java:5315)
         E/WindowManager(8870):     at java.lang.reflect.Method.invoke(Native Method)
         E/WindowManager(8870):     at java.lang.reflect.Method.invoke(Method.java:372)
         E/WindowManager(8870):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
         E/WindowManager(8870):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736)
        E/WindowManager(8870): android.view.WindowLeaked: Activity com.example.rigrunners.AssignedJobs has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{1f2c5405 V.E..... R.....ID 0,0-575,144} that was originally added here
        E/WindowManager(8870):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:367)
        E/WindowManager(8870):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
        E/WindowManager(8870):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
        E/WindowManager(8870):  at android.app.Dialog.show(Dialog.java:306)
        E/WindowManager(8870):  at com.example.rigrunners.AssignedJobs.startLoading(AssignedJobs.java:78)
        E/WindowManager(8870):  at com.example.rigrunners.AssignedJobs.load_assigned_jobs(AssignedJobs.java:198)
        E/WindowManager(8870):  at com.example.rigrunners.AssignedJobs.onCreate(AssignedJobs.java:156)
        E/WindowManager(8870):  at android.app.Activity.performCreate(Activity.java:5933)
        E/WindowManager(8870):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        E/WindowManager(8870):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        E/WindowManager(8870):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
        E/WindowManager(8870):  at android.app.ActivityThread.access0(ActivityThread.java:149)
         E/WindowManager(8870):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
         E/WindowManager(8870):     at android.os.Handler.dispatchMessage(Handler.java:102)
         E/WindowManager(8870):     at android.os.Looper.loop(Looper.java:211)
         E/WindowManager(8870):     at android.app.ActivityThread.main(ActivityThread.java:5315)
         E/WindowManager(8870):     at java.lang.reflect.Method.invoke(Native Method)
         E/WindowManager(8870):     at java.lang.reflect.Method.invoke(Method.java:372)
         E/WindowManager(8870):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
         E/WindowManager(8870):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736)
         E/AndroidRuntime(8870): FATAL EXCEPTION: main
         E/AndroidRuntime(8870): Process: com.example.rigrunners, PID: 8870
         E/AndroidRuntime(8870): java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{1f2c5405 V.E..... R.....I. 0,0-575,144} not attached to window manager
         E/AndroidRuntime(8870):    at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:386)
         E/AndroidRuntime(8870):    at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:312)
         E/AndroidRuntime(8870):    at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
          E/AndroidRuntime(8870):   at android.app.Dialog.dismissDialog(Dialog.java:349)
          E/AndroidRuntime(8870):   at android.app.Dialog.dismiss(Dialog.java:332)
          E/AndroidRuntime(8870):   at com.example.rigrunners.AssignedJobs.stopLoading(AssignedJobs.java:87)
          E/AndroidRuntime(8870):   at com.example.rigrunners.AssignedJobs.onCompleted(AssignedJobs.java:304)
         E/AndroidRuntime(8870):    at com.example.rigrunners.AssignedJobs.onCompleted(AssignedJobs.java:1)
        E/AndroidRuntime(8870):     at com.koushikdutta.async.future.SimpleFuture.handleCallbackUnlocked(SimpleFuture.java:107)
        E/AndroidRuntime(8870):     at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:141)
        E/AndroidRuntime(8870):     at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:128)
        E/AndroidRuntime(8870):     at com.koushikdutta.ion.IonRequestBuilder.run(IonRequestBuilder.java:246)
          E/AndroidRuntime(8870):   at com.koushikdutta.async.AsyncServer$RunnableWrapper.run(AsyncServer.java:57)
        E/AndroidRuntime(8870):     at android.os.Handler.handleCallback(Handler.java:739)
         E/AndroidRuntime(8870):    at android.os.Handler.dispatchMessage(Handler.java:95)
         E/AndroidRuntime(8870):    at android.os.Looper.loop(Looper.java:211)
11-27 10:41:10.630: E/AndroidRuntime(8870):     at android.app.ActivityThread.main(ActivityThread.java:5315)
            E/AndroidRuntime(8870):     at java.lang.reflect.Method.invoke(Native Method)
           E/AndroidRuntime(8870):  at java.lang.reflect.Method.invoke(Method.java:372)
         E/AndroidRuntime(8870):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
        E/AndroidRuntime(8870):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736)

分配的工作。

    public void startLoading(Activity activity) {

    proDialog = new ProgressDialog(AssignedJobs.this);
    proDialog.setMessage("Loading .Please Wait.....");
    proDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    proDialog.setCancelable(false);
    proDialog.show();

}

public void stopLoading(Activity activity) {

    if (proDialog != null) {
        {
            if (proDialog.isShowing()) {
                proDialog.dismiss();
                proDialog = null;

            }

        }
    }
}


if (common_methods.isNetworkAvailable(AssignedJobs.this)) {
        Log.i("I ma in function", "kkkkkkkkkkkkkkkkkkkkk");
        startLoading(AssignedJobs.this);
        Ion.with(AssignedJobs.this)
                .load(url/* Variables.assgned_job_url_new */).asJsonObject()
                .setCallback(new FutureCallback<JsonObject>() {

                    @Override
                    public void onCompleted(Exception e,
                            JsonObject assgned_jobs) {
                        // TODO Auto-generated method stub

                        if (e == null) {
                            team_members = "";
                            /*
                             * if
                             * (url.compareTo(Variables.assgned_job_url_new)
                             * == 0) { db.drop_table("assigned_jobs");
                             * db_identifier = 1; } else {
                             * db.drop_table("day_wise_jobs"); db_identifier
                             * = 2; }
                             */
                            db.drop_table("day_wise_jobs");
                            Log.i("assignedjob", "" + assgned_jobs);

                            if (assgned_jobs.has("team")) {
                                JsonArray team_arr = assgned_jobs
                                        .getAsJsonArray("team");
                                for (int i = 0; i < team_arr.size(); i++) {
                                    team_members = team_arr.get(i)
                                            .getAsJsonObject()
                                            .get("member_name")
                                            .getAsString()
                                            + team_members;
                                }
                            }

                            String operator, superintendent, location, bell_nipple, shell_shaker, cellar, rig_floor, compressor, createdAt, rig_id;
                            if (assgned_jobs.has("job")) {
                                JsonArray jArray = assgned_jobs.get("job")
                                        .getAsJsonArray();
                                for (int i = 0; i < jArray.size(); i++) {
                                    JsonObject jObj = jArray.get(i)
                                            .getAsJsonObject();
                                    String teamId_str = jObj.get("teamId")
                                            .getAsString();
                                    Log.i("team Id", teamId_str);

                                    // teamId=Integer.parseInt(teamId_str);
                                    // tv_team_id.setText(teamId);
                                    String jobId_str = jObj.get("id")
                                            .getAsString();
                                    // tv_job_id.setText(jobId);
                                    // jobId=Integer.parseInt(jobId_str);
                                    JsonObject jObj2 = jObj.get("rigId")
                                            .getAsJsonObject();
                                    operator = jObj2.get("operator")
                                            .getAsString();
                                    superintendent = jObj2.get(
                                            "superintendent").getAsString();
                                    createdAt = jObj2.get("createdAt")
                                            .getAsString();
                                    location = jObj2.get("location")
                                            .getAsString();
                                    JsonObject jObj_serviceId = jObj.get(
                                            "serviceId").getAsJsonObject();
                                    bell_nipple = jObj_serviceId.get(
                                            "bell_nipple").getAsString();
                                    shell_shaker = jObj_serviceId.get(
                                            "shell_shaker").getAsString();
                                    cellar = jObj_serviceId.get("cellar")
                                            .getAsString();
                                    compressor = jObj_serviceId.get(
                                            "compressor").getAsString();
                                    rig_floor = jObj_serviceId.get(
                                            "rig_floor").getAsString();
                                    rig_id = jObj_serviceId.get("id")
                                            .getAsString();
                                    /*
                                     * if (url.compareTo(Variables.
                                     * assgned_job_url_new) == 0) {
                                     * db.insert_assigned_jobs(rig_id,
                                     * createdAt, operator, superintendent,
                                     * location, team_members, shell_shaker,
                                     * rig_floor, cellar, compressor,
                                     * bell_nipple, "false", teamId_str,
                                     * jobId_str); } else {
                                     */
                                    db.insert_day_wise_jobs(rig_id,
                                            createdAt, operator,
                                            superintendent, location,
                                            team_members, shell_shaker,
                                            rig_floor, cellar, compressor,
                                            bell_nipple, "false",
                                            teamId_str, jobId_str,
                                            selected_day);
                                    /* } */

                                }

                            } /*
                             * else { Log.i("assignedjob", "" + e);
                             * Toast.makeText(AssignedJobs.this,
                             * "No jobs assigned", 1).show();
                             * 
                             * }
                             */
                            // add_Views_Dynamically();

                        stopLoading(AssignedJobs.this);}
                    }

                });

    } else {
        stopLoading(AssignedJobs.this);
        add_Views_Dynamically();

    }

尝试使用 proDialog = new ProgressDialog(getApplicationContext()); 而不是 proDialog = new ProgressDialog(AssignedJobs.this);

proDialog = new ProgressDialog(getApplicationContext());

为什么 getApplicationContext() 因为您在退出 Activity.

后试图显示 Dialog

Activity has leaked window that was originally added

查看完整原因