android活动泄露了窗口com.android.internal.policy.impl.phonewindow $ decorview问题

我正在与Android应用程序显示networking错误。

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) 

我已经search了更多…但我没有任何正确的想法来清除这一点。

我想要的是,加载这个页面时,应该添加布局,并显示对话框。

请帮我清除这个错误

注意:我也试过这个

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

谢谢你们给我很多build议 。 最后我有一个解决scheme。 那是我已经启动了两次NetErrorPage意图。 有一次,我检查了networking连接的可用性,并开始了页面启动事件的意图。 第二次,如果页面有错误,那么我已经开始了OnReceivedError事件的意图。 所以第一次对话框没有closures,在此之前第二个对话框被调用。 所以我得到一个错误。

错误原因 :在closures第一个函数之前,我调用了showInfoMessageDialog方法两次。

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

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

解决方法是在退出Activity之前,在NetErrorPage.java:114中创build的Dialog调用dismiss() ,例如在onPause()

视图有对其父Context的引用(从构造函数参数中获取)。 如果你在不破坏Dialog和其他dynamic创build的View的情况下离开一个Activity ,它们仍然持有对你的Activity引用(如果你使用Context创build的话:像new ProgressDialog(this) ),所以它不能被GC收集,导致内存泄漏。

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

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

在我的情况下完成()执行immidiately后,对话框显示。

只有在Activity的窗口状态被初始化之后,才需要启动对话框。只有在onresume之后才会发生。

所以打电话

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

在你的OnResume函数中。 不要在OnCreate中创build对话框
编辑:

用这个

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

在你的Onresume而不是showonuithread

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

设置值null为我工作

我得到这个错误,但它解决了有趣的。 首先,我得到这个错误在api级别17.当我调用一个线程(AsyncTask或其他)没有进展对话框,然后我再次调用其他线程方法使用进度对话框,我得到了这个崩溃,原因是关于使用进度对话框。

在我的情况下,有两个结果,

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

所以:

 ProgresDialog progressDialog = new ... //configure progressDialog progressDialog.show(); start firstThread { ... } ... start lastThread { ... } //be sure to finish threads progressDialog.dismiss(); 
  • 这是如此奇怪,但错误有一个能力至less对我来说摧毁自己:)
    Interesting Posts