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

后端 未结 8 737
再見小時候
再見小時候 2020-11-28 05:56

I am working with Android application to show network error.

NetErrorPage.java

package exp.app;

import android.app.Activity;
impor         


        
相关标签:
8条回答
  • 2020-11-28 06:07

    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.

    0 讨论(0)
  • 2020-11-28 06:10

    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 :-).

    0 讨论(0)
  • 2020-11-28 06:16

    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

    0 讨论(0)
  • 2020-11-28 06:19

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

    0 讨论(0)
  • 2020-11-28 06:22

    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 :)
    0 讨论(0)
  • 2020-11-28 06:27

    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();
    }
    
    0 讨论(0)
提交回复
热议问题