在一个ListView的Item中,点击一个Item内的一张图片放大,我在ListView的Adapter中对该图片设置了监听,点击时弹出一个Dialog。然后切屏,报以下错误:
12-24 07:39:41.699: ERROR/WindowManager(325): Activity com.com.adapterdialog.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44e94f30 that was originally added here
12-24 07:39:41.699: ERROR/WindowManager(325): android.view.WindowLeaked: Activity com.com.adapterdialog.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44e94f30 that was originally added here
12-24 07:39:41.699: ERROR/WindowManager(325): at android.view.ViewRoot.<init>(ViewRoot.java:247)
12-24 07:39:41.699: ERROR/WindowManager(325): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
12-24 07:39:41.699: ERROR/WindowManager(325): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
12-24 07:39:41.699: ERROR/WindowManager(325): at android.view.Window$LocalWindowManager.addView(Window.java:424)
12-24 07:39:41.699: ERROR/WindowManager(325): at android.app.Dialog.show(Dialog.java:241)
12-24 07:39:41.699: ERROR/WindowManager(325): at com.com.adapterdialog.adapter.MainAdapter.imageDialog(MainAdapter.java:95)
12-24 07:39:41.699: ERROR/WindowManager(325): at com.com.adapterdialog.adapter.MainAdapter.access$2(MainAdapter.java:85)
12-24 07:39:41.699: ERROR/WindowManager(325): at com.com.adapterdialog.adapter.MainAdapter$1.onClick(MainAdapter.java:73)
12-24 07:39:41.699: ERROR/WindowManager(325): at android.view.View.performClick(View.java:2408)
12-24 07:39:41.699: ERROR/WindowManager(325): at android.view.View$PerformClick.run(View.java:8816)
12-24 07:39:41.699: ERROR/WindowManager(325): at android.os.Handler.handleCallback(Handler.java:587)
12-24 07:39:41.699: ERROR/WindowManager(325): at android.os.Handler.dispatchMessage(Handler.java:92)
12-24 07:39:41.699: ERROR/WindowManager(325): at android.os.Looper.loop(Looper.java:123)
12-24 07:39:41.699: ERROR/WindowManager(325): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-24 07:39:41.699: ERROR/WindowManager(325): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 07:39:41.699: ERROR/WindowManager(325): at java.lang.reflect.Method.invoke(Method.java:521)
12-24 07:39:41.699: ERROR/WindowManager(325): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-24 07:39:41.699: ERROR/WindowManager(325): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-24 07:39:41.699: ERROR/WindowManager(325): at dalvik.system.NativeStart.main(Native Method)
解决方法:
在定义Activity的地方加上这个属性:android:configChanges="keyboardHidden|orientation"。当在xml文件中配置了android:configChanges="keyboardHidden|orientation"属性,在横紧屏切换的时候不会重新执行Activity的onCreate方法,只是执行onConfigurationChanged方法,否则切换的时候会重新执行onCreate方法。