环境:
在PopopWindow里面使用Spinner组件,会报错如下:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sg.huosansihu, PID: 30027
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@e3124a is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:709)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:319)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1272)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1119)
at android.widget.ListPopupWindow.show(ListPopupWindow.java:663)
at android.widget.Spinner$DropdownPopup.show(Spinner.java:1236)
at android.widget.Spinner.performClick(Spinner.java:765)
at androidx.appcompat.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:443)
at android.view.View$PerformClick.run(View.java:21559)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:768)
解决方法:
在Spinner组件XML属性里面添加如下内容:
android:spinnerMode="dialog"