Android的活动已泄漏窗口com.android.internal.policy.impl.ph

2019-06-17 18:47发布

我与Android应用程序的工作,以显示网络错误。

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;
        }
    }    
}

但我发现了如下的错误,

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.<init>(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)

我已经搜索更多...但我没有任何正确的理念来清除此。

我要的是,加载此页面时,布局应补充和应显示对话框。

请帮我清除此错误

注:我也试过这种

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

    }

Answer 1:

感谢你们给了我很多建议 。 最后,我得到了一个解决方案。 这是我已经开始NetErrorPage意图两次。 有一次,我已经检查了网络连接的可用性,并开始有意以页开始的事件。 第二次,如果页面有错误的话,我已经开始在OnReceivedError事件的意图。 所以第一次对话不是封闭的,在此之前,在第二个对话框被调用。 所以,我得到了一个错误。

原因错误 :我已经关闭的第一个前称为showInfoMessageDialog方法两次。

现在,我已删除了第二个电话,:-)清除错误。



Answer 2:

改变这种dialog.cancel();dialog.dismiss();

解决的办法是打电话dismiss()Dialog退出之前,你在NetErrorPage.java:114创建Activity ,例如在onPause()

意见有他们的父母参考Context (从构造函数的参数拍摄)。 如果你离开一个Activity ,而不破坏Dialog S和其它动态创建的View S,他们仍然坚持这一参考您的Activity (如果你用这个作为创建Context :像new ProgressDialog(this) ),所以它不能被GC回收,引起了内存泄漏。



Answer 3:

在我的情况下finish()立即执行的对话框显示后。



Answer 4:

请尝试这种方式,让我知道:

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();
}


Answer 5:

需要该对话框只有在活动的窗口状态初始化这只发生后的onResume启动。

于是呼

runOnUIthread(new Runnable(){

    showInfoMessageDialog("Please check your network connection","Network Alert");
});

在你的onResume功能。 不要在创建对话框的OnCreate
编辑:

用这个

Handler h = new Handler();

h.postDelayed(new Runnable(){

        showInfoMessageDialog("Please check your network connection","Network Alert");
    },500);

在你的onResume代替showonuithread



Answer 6:

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

其值设置为null对我的作品



Answer 7:

我得到这个错误,但它是解决有趣。 作为第一,我当我调用线程(的AsyncTask或其他)不进度对话框,然后我打电话的其他线程的方法再次使用进度对话框在拿到API级别17.这个错误,我得到的崩溃,原因是有关进度对话框的使用。

在我的情况下,有两种结果;

  • 我把show(); 第一个线程之前进度对话框的方法启动,然后我花了dismiss(); 最后一个线程结束之前,进度对话框的方法。

所以:

ProgresDialog progressDialog = new ...

//configure progressDialog 

progressDialog.show();

start firstThread {
...
}

...

start lastThread {
...
} 

//be sure to finish threads

progressDialog.dismiss();
  • 这就是这么奇怪,但这个错误有能力约至少摧毁本身对我来说:)


文章来源: android activity has leaked window com.android.internal.policy.impl.phonewindow$decorview Issue