我使用ACRA报告应用程序崩溃。我得到了一个视图没有附加到窗口管理器错误消息,并认为我已经通过包装pDialog.dismiss()修复了它;在if语句中:

if (pDialog!=null) 
{
    if (pDialog.isShowing()) 
    {
        pDialog.dismiss();   
    }
}

它减少了我收到的未附加到窗口管理器崩溃的视图数量,但我仍然得到一些,我不确定如何解决它。

错误信息:

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:425)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:327)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:83)
at android.app.Dialog.dismissDialog(Dialog.java:330)
at android.app.Dialog.dismiss(Dialog.java:312)
at com.package.class$LoadAllProducts.onPostExecute(class.java:624)
at com.package.class$LoadAllProducts.onPostExecute(class.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)

代码片段:

class LoadAllProducts extends AsyncTask<String, String, String> 
{

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
        pDialog = new ProgressDialog(CLASS.this);
        pDialog.setMessage("Loading. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) 
    {
        // Building Parameters
        doMoreStuff("internet");
        return null;
    }


    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) 
    {
         // dismiss the dialog after getting all products
         if (pDialog!=null) 
         {
                if (pDialog.isShowing()) 
                {
                    pDialog.dismiss();   //This is line 624!    
                }
         }
         something(note);
    }
}

清单:

    <activity
        android:name="pagename.CLASS" 
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"            
        android:label="@string/name" >
    </activity>

为了阻止撞车,我错过了什么?


当前回答

您可以尝试检查这个解决方案或尝试下面的解决方案。

if (pDialog instanceof Activity && !((Activity) mContext).isFinishing())
        pDialog.show();

其他回答

如何重现错误:

在您的设备上启用此选项:设置->开发人员选项->不要保留活动。 当AsyncTask正在执行并且ProgressDialog正在显示时,按Home键。

Android操作系统会在活动被隐藏时立即销毁它。当onPostExecute被调用时,Activity将处于“完成”状态,并且ProgressDialog不会附加到Activity。

如何解决:

在onPostExecute方法中检查活动状态。 取消onDestroy方法中的ProgressDialog。否则,将抛出android.view. windowleaks异常。此异常通常来自活动结束时仍处于活动状态的对话框。

试试这个固定的代码:

public class YourActivity extends Activity {

    private void showProgressDialog() {
        if (pDialog == null) {
            pDialog = new ProgressDialog(StartActivity.this);
            pDialog.setMessage("Loading. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
        }
        pDialog.show();
    }

    private void dismissProgressDialog() {
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        dismissProgressDialog();
        super.onDestroy();
    }

    class LoadAllProducts extends AsyncTask<String, String, String> {

        // Before starting background thread Show Progress Dialog
        @Override
        protected void onPreExecute() {
            showProgressDialog();
        }

        //getting All products from url
        protected String doInBackground(String... args) {
            doMoreStuff("internet");
            return null;
        }

        // After completing background task Dismiss the progress dialog
        protected void onPostExecute(String file_url) {
            if (YourActivity.this.isDestroyed()) { // or call isFinishing() if min sdk version < 17
                return;
            }
            dismissProgressDialog();
            something(note);
        }
    }
}

重写onConfigurationChanged并取消进度对话框。 如果进度对话框在纵向中创建,并在横向中解散,那么它将抛出未附加到窗口管理器的视图错误。

同时在onPause(), onBackPressed和onDestroy方法中停止进度条和异步任务。

if(asyncTaskObj !=null && asyncTaskObj.getStatus().equals(AsyncTask.Status.RUNNING)){

    asyncTaskObj.cancel(true);

}

对于在片段中创建的对话框,我使用以下代码:

ProgressDialog myDialog = new ProgressDialog(getActivity());
myDialog.setOwnerActivity(getActivity());
...
Activity activity = myDialog.getOwnerActivity();
if( activity!=null && !activity.isFinishing()) {
    myDialog.dismiss();
}

我使用这种模式来处理片段可能从活动中分离的情况。

@Override
public void onPause() {
    super.onPause();

    if(pDialog != null)
        pDialog .dismiss();
    pDialog = null;
}

提到这一点。

上面的解决方案都不适合我,因为我使用自定义对话类,我不想覆盖停止/销毁方法在我所有的活动,更容易添加一个参数到我的类,并从实例发送它,这个解决方案适合我

class CustomLoadingClass(var activity: Activity?, var lifecycle: Lifecycle? = null) {

lateinit var dialog: Dialog

fun show(cancelable: Boolean = false, title: String? = null) {
    lifecycle?.let {
        val defaultLifecycleObserver = object : DefaultLifecycleObserver {
            override fun onCreate(owner: LifecycleOwner) {
                super.onCreate(owner)
                activity?.let { it ->
                    if (!it.isFinishing && !it.isDestroyed) {
                        dialog = Dialog(it, android.R.style.Theme_Translucent_NoTitleBar)
                        dialog.setContentView(it.layoutInflater.inflate(R.layout.full_screen_progress_bar,null)!!)
                        dialog.setCancelable(cancelable)
                        dialog.show()
                    }
                }
            }
            override fun onPause(owner: LifecycleOwner) {
                super.onPause(owner)
                dismiss()
            }

            override fun onStop(owner: LifecycleOwner) {
                super.onStop(owner)
                dismiss()
            }

            override fun onDestroy(owner: LifecycleOwner) {
                super.onDestroy(owner)
                dismiss()
            }
        }
        it.addObserver(defaultLifecycleObserver)
    }
}

fun dismiss() {
    activity?.let { it ->
         if (!it.isFinishing && !it.isDestroyed) 
           dialog.dismiss()
    }
  }
}