android activity has leaked window com.android.internal.policy.impl.phonewindow$decorview Issue

匿名 (未验证) 提交于 2019-12-03 02:19:01

问题:

I am working with Android application to show network error.

NetErrorPage.java

package exp.app;  import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;  public class NetErrorPage extends Activity implements OnClickListener {          @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.neterrorlayout);         Button reload=(Button)findViewById(R.id.btnReload);         reload.setOnClickListener(this);             showInfoMessageDialog("Please check your network connection","Network Alert");      }      public void onClick(View arg0)                      {             if(isNetworkAvailable())             {                                    Intent myIntent = new Intent((Activity)NetErrorPage.this, MainActivity.class);                    myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);                               ((Activity)NetErrorPage.this).startActivity(myIntent);                 finish();             }             else                 showInfoMessageDialog("Please check your network connection","Network Alert");     }      public void showInfoMessageDialog(String message,String title)        {         AlertDialog alertDialog = new AlertDialog.Builder(NetErrorPage.this).create();         alertDialog.setTitle("Network Alert");         alertDialog.setMessage(message);         alertDialog.setButton("OK",                 new DialogInterface.OnClickListener() {                     public void onClick(DialogInterface dialog,int which)                      {                            dialog.cancel();                     }                 });                     alertDialog.show();     }   private boolean isNetworkAvailable()     {         NetworkInfo ActiveNetwork;         @SuppressWarnings("unused")         String IsNetworkConnected;         @SuppressWarnings("unused")         String ConnectionType;         ConnectivityManager connectivitymanager;         connectivitymanager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);                 try         {                        ActiveNetwork=connectivitymanager.getActiveNetworkInfo();             ConnectionType=ActiveNetwork.getTypeName();              IsNetworkConnected=String.valueOf(ActiveNetwork.getState());             return true;                                 }         catch(Exception error)         {                 return false;         }     }     } 

but i'm getting the error as below,

08-17 11:59:08.019: E/WindowManager(16460): Activity exp.app.NetErrorPage has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40534a18 that was originally added here 08-17 11:59:08.019: E/WindowManager(16460): android.view.WindowLeaked: Activity exp.app.NetErrorPage has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40534a18 that was originally added here 08-17 11:59:08.019: E/WindowManager(16460):     at android.view.ViewRoot.(ViewRoot.java:263) 08-17 11:59:08.019: E/WindowManager(16460):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 08-17 11:59:08.019: E/WindowManager(16460):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 08-17 11:59:08.019: E/WindowManager(16460):     at android.view.Window$LocalWindowManager.addView(Window.java:424) 08-17 11:59:08.019: E/WindowManager(16460):     at android.app.Dialog.show(Dialog.java:241) 08-17 11:59:08.019: E/WindowManager(16460):     at sync.directtrac.NetError.showInfoMessageDialog(NetErrorPage.java:114) 08-17 11:59:08.019: E/WindowManager(16460):     at sync.directtrac.NetError.onCreate(NetErrorPage.java:26) 08-17 11:59:08.019: E/WindowManager(16460):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-17 11:59:08.019: E/WindowManager(16460):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 08-17 11:59:08.019: E/WindowManager(16460):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 08-17 11:59:08.019: E/WindowManager(16460):     at android.app.ActivityThread.access$1500(ActivityThread.java:117) 08-17 11:59:08.019: E/WindowManager(16460):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 08-17 11:59:08.019: E/WindowManager(16460):     at android.os.Handler.dispatchMessage(Handler.java:99) 08-17 11:59:08.019: E/WindowManager(16460):     at android.os.Looper.loop(Looper.java:130) 08-17 11:59:08.019: E/WindowManager(16460):     at android.app.ActivityThread.main(ActivityThread.java:3687) 08-17 11:59:08.019: E/WindowManager(16460):     at java.lang.reflect.Method.invokeNative(Native Method) 08-17 11:59:08.019: E/WindowManager(16460):     at java.lang.reflect.Method.invoke(Method.java:507) 08-17 11:59:08.019: E/WindowManager(16460):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 08-17 11:59:08.019: E/WindowManager(16460):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 08-17 11:59:08.019: E/WindowManager(16460):     at dalvik.system.NativeStart.main(Native Method) 

I have searched more... but i don't have any right idea to clear this.

What i want is, when loading this page, layout should be added and dialog should be shown.

Please help me to clear this error

Note: I have tried this also

@Override     protected void onResume() {     super.onResume();         runOnUiThread(new Runnable() {             public void run() {                 showInfoMessageDialog("Please check your network connection","Network Alert");             }         });      } 

回答1:

Thank you Guys to give me many suggestions. Finally I got a solution. That is i have started the NetErrorPage intent two times. One time, i have checked the net connection availability and started the intent in page started event. second time, if the page has error, then i have started the intent in OnReceivedError event. So the first time dialog is not closed, before that the second dialog is called. So that i got a error.

Reason for the Error: I have called the showInfoMessageDialog method two times before closing the first one.

Now I have removed the second call and Cleared error :-).



回答2:

Change this dialog.cancel(); to dialog.dismiss();

The solution is to call dismiss() on the Dialog you created in NetErrorPage.java:114 before exiting the Activity, e.g. in onPause().

Views have a reference to their parent Context (taken from constructor argument). If you leave an Activity without destroying Dialogs and other dynamically created Views, they still hold this reference to your Activity (if you created with this as Context: like new ProgressDialog(this)), so it cannot be collected by the GC, causing a memory leak.



回答3:

Please try this Way And Let me know :

Context mContext; @Override public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.neterrorlayout);     mContext=NetErrorPage.this;    Button reload=(Button)findViewById(R.id.btnReload);    reload.setOnClickListener(this);        showInfoMessageDialog("Please check your network connection","Network Alert");  } public void showInfoMessageDialog(String message,String title) {    new AlertDialog.Builder(mContext)    .setTitle("Network Alert");    .setMessage(message);    .setButton("OK",new DialogInterface.OnClickListener() {        public void onClick(DialogInterface dialog,int which)         {              dialog.cancel();        }    })    .show(); } 


回答4:

In my case finish() executed immidiately after a dialog has shown.



回答5:

The dialog needs to be started only after the window states of the Activity are initialized This happens only after onresume.

So call

runOnUIthread(new Runnable(){      showInfoMessageDialog("Please check your network connection","Network Alert"); }); 

in your OnResume function. Do not create dialogs in OnCreate
Edit:

use this

Handler h = new Handler();  h.postDelayed(new Runnable(){          showInfoMessageDialog("Please check your network connection","Network Alert");     },500); 

in your Onresume instead of showonuithread



回答6:

 @Override     protected void onPostExecute(final Boolean success) {         mProgressDialog.dismiss();         mProgressDialog = null; 

setting the value null works for me



回答7:

I got this error but it is resolved interesting. As first, i got this error at api level 17. When i call a thread (AsyncTask or others) without progress dialog then i call an other thread method again using progress dialog, i got that crash and the reason is about usage of progress dialog.

In my case, there are two results that;

  • I took show(); method of progress dialog before first thread starts then i took dismiss(); method of progress dialog before last thread ends.

So :

ProgresDialog progressDialog = new ...  //configure progressDialog   progressDialog.show();  start firstThread { ... }  ...  start lastThread { ... }   //be sure to finish threads  progressDialog.dismiss(); 
  • This is so strange but that error has an ability about destroy itself at least for me :)


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