ProgressDialog 导致应用程序崩溃 android

ProgressDialog causes app to crash in android

我正在尝试在 android 中创建一个简单的 CRUD 应用程序。

每当我登录时,应用程序都会崩溃,但在内部它确实让我登录。当我注销时出现同样的问题。我在 progressDialog.dismiss().

上收到错误

这是Asynctask中的代码class:

    @Override
protected void onPreExecute(){
    super.onPreExecute();
    path = ctx.getString(R.string.url);
    progressDialog = new ProgressDialog(ctx);
    progressDialog.setTitle("Signing In");
    progressDialog.setMessage("Validating your credentials!");
    //progressDialog.setCancelable(false);
    progressDialog.show();
}

这是我在 onPostExecute 方法中的代码:

@Override
protected void onPostExecute(String result){
    super.onPostExecute(result);
    if(progressDialog!=null){
        if(progressDialog.isShowing()){
            progressDialog.dismiss();
        }
        progressDialog = null;
    }
     //some more code
}

我在 progressDialog.show() 行收到 window 泄漏错误,在 progressDialog.dismiss() 行收到 IllegalArgumentException 错误。

这是我的 activity 日志:

06-07 02:14:11.155 1828-1828/com.example.thadaninilesh.productapp E/WindowManager: android.view.WindowLeaked: Activity com.example.thadaninilesh.productapp.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{2b714c45 V.E..... R......D 0,0-959,322} that was originally added here
                                                                                   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
                                                                                   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
                                                                                   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
                                                                                   at android.app.Dialog.show(Dialog.java:298)
                                                                                   at com.example.thadaninilesh.productapp.BackgroundUserLogoutTask.onPreExecute(BackgroundUserLogoutTask.java:56)
                                                                                   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:591)
                                                                                   at android.os.AsyncTask.execute(AsyncTask.java:539)
                                                                                   at com.example.thadaninilesh.productapp.DashboardFragment.onClick(DashboardFragment.java:61)
                                                                                   at android.view.View.performClick(View.java:4780)
                                                                                   at android.view.View$PerformClick.run(View.java:19866)
                                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                   at android.os.Looper.loop(Looper.java:135)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

06-07 02:14:12.447 1828-1828/com.example.thadaninilesh.productapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.thadaninilesh.productapp, PID: 1828
                                                                                java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{2b714c45 V.E..... R......D 0,0-959,322} not attached to window manager
                                                                                    at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:396)
                                                                                    at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:322)
                                                                                    at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)
                                                                                    at android.app.Dialog.dismissDialog(Dialog.java:341)
                                                                                    at android.app.Dialog.dismiss(Dialog.java:324)
                                                                                    at com.example.thadaninilesh.productapp.BackgroundUserLogoutTask.onPostExecute(BackgroundUserLogoutTask.java:117)
                                                                                    at com.example.thadaninilesh.productapp.BackgroundUserLogoutTask.onPostExecute(BackgroundUserLogoutTask.java:35)
                                                                                    at android.os.AsyncTask.finish(AsyncTask.java:636)
                                                                                    at android.os.AsyncTask.access0(AsyncTask.java:177)
                                                                                    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

看起来您显示进度对话框的 activity/fragment 在关闭对话框之前已被销毁。所以这会让你崩溃

IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView not attached to window manager

因此,在您完成 activity 之前,关闭您的进度对话框。