[Android活动已泄漏com.android.internal.policy.impl.phonewindow $ decorview问题窗口

寸阳分阴须爱惜,休负春色与时光。这篇文章主要讲述[Android活动已泄漏com.android.internal.policy.impl.phonewindow $ decorview问题窗口相关的知识,希望能为你提供帮助。
我正在使用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"); } }); }

答案谢谢你们给我很多建议。终于我找到了解决方案。那就是我已经两次启动了NetErrorPage意图。有一次,我检查了网络连接的可用性,并在页面启动事件中启动了意图。第二次,如果页面有错误,那么我已经在OnReceivedError事件中启动了意图。因此,在第二个对话框被调用之前,第一次对话框没有关闭。这样我就出错了。
错误原因:我已经调用了showInfoMessageDialog方法在关闭第一个之前两次。
现在我已删除第二个呼叫并清除了错误:-)。
另一答案[将此dialog.cancel(); 更改为dialog.dismiss(); ] > >
解决方案是在退出dismiss()之前,在NetErrorPage.java:114
中创建的Dialog上调用Activity。在onPause()中。视图具有对其父级Context的引用(取自构造函数参数)。如果在不破坏Activity和其他动态创建的Dialog的情况下保留View,它们仍然保留对Activity的引用(如果您使用Context来创建此引用,例如new ProgressDialog(this)),那么GC无法收集它,从而导致内存泄漏。
另一答案在我的情况下,显示对话框后立即执行finish()
另一答案请尝试这种方式,让我知道:
另一答案仅在初始化“活动”的窗口状态后才需要启动对话框仅在onresume之后发生。
另一答案
@Override protected void onPostExecute(final Boolean success) { mProgressDialog.dismiss(); mProgressDialog = null;

另一答案我遇到了这个错误,但是很有趣。首先,我在api级别17遇到此错误。当我调用没有进度对话框的线程(AsyncTask或其他线程),然后再次使用进度对话框调用其他线程方法时,出现崩溃,原因是使用进度对话框。
另一答案【[Android活动已泄漏com.android.internal.policy.impl.phonewindow $ decorview问题窗口】我解决此问题的方法是不在对话框中调用意图。****相应地使用适用于活动或片段的语法@Overridepublic void onClick(DialogInterface dialog,int which){checkvariable = true; getActivity()。finish(); }

    推荐阅读