Spinner下拉列表和屏幕方向更改问题

时间:2011-03-08 14:22:44

标签: android spinner screen-orientation

我有一个微调器下拉列表和更改方向的问题。

在我的活动中,我显示了一个对话框,其中有两个微调器。当向用户显示对话框并且用户更改屏幕方向时,一切正常 - 在我的onPause中,我只是简单地关闭对话框。

如果用户点击微调器,则在更改方向之前会向用户显示一个下拉列表。但是当用户没有从下拉列表中选择任何内容时,我遇到了一个错误,只是在更改方向之前将列表展开。

我尝试过很多方法来解决这个问题(例如spinner.setSelection(0)中的onSaveInstanceState()等)。我的想法是当用户没有选择项目并且发生方向更改时,从代码中关闭下拉列表。

错误大约是Android.WindowLeaked

E/WindowManager( 2455): Activity pl.myapp.WebViewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45041e60 that was originally added here
E/WindowManager( 2455): android.view.WindowLeaked: Activity pl.myapp.WebViewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45041e60 that was originally added here
E/WindowManager( 2455):     at android.view.ViewRoot.<init>(ViewRoot.java:247)
E/WindowManager( 2455):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
E/WindowManager( 2455):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/WindowManager( 2455):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
E/WindowManager( 2455):     at android.app.Dialog.show(Dialog.java:241)
E/WindowManager( 2455):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
E/WindowManager( 2455):     at android.widget.Spinner.performClick(Spinner.java:257)
E/WindowManager( 2455):     at android.view.View$PerformClick.run(View.java:8816)
E/WindowManager( 2455):     at android.os.Handler.handleCallback(Handler.java:587)
E/WindowManager( 2455):     at android.os.Handler.dispatchMessage(Handler.java:92)
E/WindowManager( 2455):     at android.os.Looper.loop(Looper.java:123)
E/WindowManager( 2455):     at android.app.ActivityThread.main(ActivityThread.java:4627)
E/WindowManager( 2455):     at java.lang.reflect.Method.invokeNative(Native Method)
E/WindowManager( 2455):     at java.lang.reflect.Method.invoke(Method.java:521)
E/WindowManager( 2455):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/WindowManager( 2455):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/WindowManager( 2455):     at dalvik.system.NativeStart.main(Native Method)

此外,出现此错误:

D/AndroidRuntime( 2455): Shutting down VM
W/dalvikvm( 2455): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime( 2455): FATAL EXCEPTION: main
E/AndroidRuntime( 2455): java.lang.IllegalArgumentException: View not attached to window manager
E/AndroidRuntime( 2455):    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
E/AndroidRuntime( 2455):    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
E/AndroidRuntime( 2455):    at android.view.Window$LocalWindowManager.removeView(Window.java:432)
E/AndroidRuntime( 2455):    at android.app.Dialog.dismissDialog(Dialog.java:278)
E/AndroidRuntime( 2455):    at android.app.Dialog.access$000(Dialog.java:71)
E/AndroidRuntime( 2455):    at android.app.Dialog$1.run(Dialog.java:111)
E/AndroidRuntime( 2455):    at android.app.Dialog.dismiss(Dialog.java:268)
E/AndroidRuntime( 2455):    at android.widget.Spinner.onDetachedFromWindow(Spinner.java:86)
E/AndroidRuntime( 2455):    at android.view.View.dispatchDetachedFromWindow(View.java:6033)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1158)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewRoot.dispatchDetachedFromWindow(ViewRoot.java:1630)
E/AndroidRuntime( 2455):    at android.view.ViewRoot.doDie(ViewRoot.java:2671)
E/AndroidRuntime( 2455):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1948)
E/AndroidRuntime( 2455):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2455):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2455):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 2455):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2455):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 2455):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 2455):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 2455):    at dalvik.system.NativeStart.main(Native Method)

修改

解决方案:

最后我找到了导致此*&amp;#$%错误的原因。在我的活动清单中我有 android:configChanges =“orientation”,当我把它改成android时:configChanges =“keyboardHidden | orientation”问题消失了!

Thx 4帮助!

3 个答案:

答案 0 :(得分:1)

当显示与微调器的对话框时,我有idea to prevent orientation changes,但发现there is no reliable way to do that。我找到的最佳解决方案是replace spinner with a button

答案 1 :(得分:0)

答案在这里:app crashes when alert dialog is open and phone(emulator) changes its orientation。看看MegaMind的答案,以帮助解决这个问题。

答案 2 :(得分:0)

这是因为在调用dialog.dismiss()时没有在微调器上调用onDetachedFromWindow()。解决方案是创建一个简单的自定义微调器类来公开此方法。

 public class DialogSpinner extends Spinner {

     public DialogSpinner(Context context, AttributeSet attrs) {
         super(context, attrs);
     }

     @Override
     public void onDetachedFromWindow() {
         super.onDetachedFromWindow();
     }
 }

现在onDetachedFromWindow()方法是公开可用的。然后在对话框类中重写onPause()方法。

 @Override
 public void onPause() {
     mYourSpinner.onDetachedFromWindow();
     super.onPause();
 }

这肯定是一种解决方法,但似乎可以解决问题。